| 1 | package g8scip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ |
| 2 | |
| 3 | import ( |
| 4 | "contextscip-go gomod github.com/golang/go/src go1.23.3 context/" |
| 5 | "net/httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/" |
| 6 | "stringsscip-go gomod github.com/golang/go/src go1.23.3 strings/" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | // AuthorizationHeader is the header in which g8 looks for the authorization bearer token |
| 11 | AuthorizationHeaderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationHeader. = "Authorization" |
| 12 | |
| 13 | // DefaultUnauthorizedResponseBody is the default response body returned if a request was sent with a missing or invalid token |
| 14 | DefaultUnauthorizedResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/DefaultUnauthorizedResponseBody. = "token is missing or invalid" |
| 15 | |
| 16 | // DefaultTooManyRequestsResponseBody is the default response body returned if a request exceeded the allowed rate limit |
| 17 | DefaultTooManyRequestsResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/DefaultTooManyRequestsResponseBody. = "too many requests" |
| 18 | |
| 19 | // TokenContextKey is the key used to store the client's token in the context. |
| 20 | TokenContextKeyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/TokenContextKey. = "g8.token" |
| 21 | |
| 22 | // DataContextKey is the key used to store the client's data in the context. |
| 23 | DataContextKeyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/DataContextKey. = "g8.data" |
| 24 | ) |
| 25 | |
| 26 | // Gate is lock to the front door of your API, letting only those you allow through. |
| 27 | type Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate# struct { |
| 28 | authorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#authorizationService. *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService# |
| 29 | unauthorizedResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#unauthorizedResponseBody. []byte |
| 30 | |
| 31 | customTokenExtractorFuncscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#customTokenExtractorFunc. func(request *httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#) string |
| 32 | |
| 33 | rateLimiterscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#rateLimiter. *RateLimiterscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter# |
| 34 | tooManyRequestsResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#tooManyRequestsResponseBody. []byte |
| 35 | } |
| 36 | |
| 37 | // Deprecated: use New instead. |
| 38 | func NewGatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/NewGate().(authorizationService *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#) *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate# { |
| 39 | return &Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#{ |
| 40 | authorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#authorizationService.: authorizationService, |
| 41 | unauthorizedResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#unauthorizedResponseBody.: []byte(DefaultUnauthorizedResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/DefaultUnauthorizedResponseBody.), |
| 42 | tooManyRequestsResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#tooManyRequestsResponseBody.: []byte(DefaultTooManyRequestsResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/DefaultTooManyRequestsResponseBody.), |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // New creates a new Gate. |
| 47 | func Newscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/New().() *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate# { |
| 48 | return &Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#{ |
| 49 | unauthorizedResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#unauthorizedResponseBody.: []byte(DefaultUnauthorizedResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/DefaultUnauthorizedResponseBody.), |
| 50 | tooManyRequestsResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#tooManyRequestsResponseBody.: []byte(DefaultTooManyRequestsResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/DefaultTooManyRequestsResponseBody.), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // WithAuthorizationService sets the authorization service to use. |
| 55 | // |
| 56 | // If there is no authorization service, Gate will not enforce authorization. |
| 57 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) WithAuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#WithAuthorizationService().(authorizationService *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#) *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate# { |
| 58 | gate.authorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#authorizationService. = authorizationService |
| 59 | return gate |
| 60 | } |
| 61 | |
| 62 | // WithCustomUnauthorizedResponseBody sets a custom response body when Gate determines that a request must be blocked |
| 63 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) WithCustomUnauthorizedResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#WithCustomUnauthorizedResponseBody().(unauthorizedResponseBody []byte) *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate# { |
| 64 | gate.unauthorizedResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#unauthorizedResponseBody. = unauthorizedResponseBody |
| 65 | return gate |
| 66 | } |
| 67 | |
| 68 | // WithCustomTokenExtractor allows the specification of a custom function to extract a token from a request. |
| 69 | // If a custom token extractor is not specified, the token will be extracted from the Authorization header. |
| 70 | // |
| 71 | // For instance, if you're using a session cookie, you can extract the token from the cookie like so: |
| 72 | // |
| 73 | // authorizationService := g8.NewAuthorizationService() |
| 74 | // customTokenExtractorFunc := func(request *http.Request) string { |
| 75 | // sessionCookie, err := request.Cookie("session") |
| 76 | // if err != nil { |
| 77 | // return "" |
| 78 | // } |
| 79 | // return sessionCookie.Value |
| 80 | // } |
| 81 | // gate := g8.New().WithAuthorizationService(authorizationService).WithCustomTokenExtractor(customTokenExtractorFunc) |
| 82 | // |
| 83 | // You would normally use this with a client provider that matches whatever need you have. |
| 84 | // For example, if you're using a session cookie, your client provider would retrieve the user from the session ID |
| 85 | // extracted by this custom token extractor. |
| 86 | // |
| 87 | // Note that for the sake of convenience, the token extracted from the request is passed the protected handlers request |
| 88 | // context under the key TokenContextKey. This is especially useful if the token is in fact a session ID. |
| 89 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) WithCustomTokenExtractorscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#WithCustomTokenExtractor().(customTokenExtractorFunc func(request *httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#) string) *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate# { |
| 90 | gate.customTokenExtractorFuncscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#customTokenExtractorFunc. = customTokenExtractorFunc |
| 91 | return gate |
| 92 | } |
| 93 | |
| 94 | // WithRateLimit adds rate limiting to the Gate |
| 95 | // |
| 96 | // If you just want to use a gate for rate limiting purposes: |
| 97 | // |
| 98 | // gate := g8.New().WithRateLimit(50) |
| 99 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) WithRateLimitscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#WithRateLimit().(maximumRequestsPerSecond int) *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate# { |
| 100 | gate.rateLimiterscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#rateLimiter. = NewRateLimiterscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/NewRateLimiter().(maximumRequestsPerSecond) |
| 101 | return gate |
| 102 | } |
| 103 | |
| 104 | // Protect secures a handler, requiring requests going through to have a valid Authorization Bearer token. |
| 105 | // Unlike ProtectWithPermissions, Protect will allow access to any registered tokens, regardless of their permissions |
| 106 | // or lack thereof. |
| 107 | // |
| 108 | // Example: |
| 109 | // |
| 110 | // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithToken("token")) |
| 111 | // router := http.NewServeMux() |
| 112 | // // Without protection |
| 113 | // router.Handle("/handle", yourHandler) |
| 114 | // // With protection |
| 115 | // router.Handle("/handle", gate.Protect(yourHandler)) |
| 116 | // |
| 117 | // The token extracted from the request is passed to the handlerFunc request context under the key TokenContextKey |
| 118 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) Protectscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#Protect().(handler httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler#) httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler# { |
| 119 | return gate.ProtectWithPermissionsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectWithPermissions().(handler, nil) |
| 120 | } |
| 121 | |
| 122 | // ProtectWithPermissions secures a handler, requiring requests going through to have a valid Authorization Bearer token |
| 123 | // as well as a slice of permissions that must be met. |
| 124 | // |
| 125 | // Example: |
| 126 | // |
| 127 | // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClient(g8.NewClient("token").WithPermission("ADMIN"))) |
| 128 | // router := http.NewServeMux() |
| 129 | // // Without protection |
| 130 | // router.Handle("/handle", yourHandler) |
| 131 | // // With protection |
| 132 | // router.Handle("/handle", gate.ProtectWithPermissions(yourHandler, []string{"admin"})) |
| 133 | // |
| 134 | // The token extracted from the request is passed to the handlerFunc request context under the key TokenContextKey |
| 135 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) ProtectWithPermissionsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectWithPermissions().(handler httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler#, permissions []string) httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler# { |
| 136 | return gate.ProtectFuncWithPermissionsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectFuncWithPermissions().(func(writer httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/ResponseWriter#, request *httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#) { |
| 137 | handler.ServeHTTPscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler#ServeHTTP().(writer, request) |
| 138 | }, permissions) |
| 139 | } |
| 140 | |
| 141 | // ProtectWithPermission does the same thing as ProtectWithPermissions, but for a single permission instead of a |
| 142 | // slice of permissions |
| 143 | // |
| 144 | // See ProtectWithPermissions for further documentation |
| 145 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) ProtectWithPermissionscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectWithPermission().(handler httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler#, permission string) httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler# { |
| 146 | return gate.ProtectFuncWithPermissionsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectFuncWithPermissions().(func(writer httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/ResponseWriter#, request *httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#) { |
| 147 | handler.ServeHTTPscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler#ServeHTTP().(writer, request) |
| 148 | }, []string{permission}) |
| 149 | } |
| 150 | |
| 151 | // ProtectFunc secures a handlerFunc, requiring requests going through to have a valid Authorization Bearer token. |
| 152 | // Unlike ProtectFuncWithPermissions, ProtectFunc will allow access to any registered tokens, regardless of their |
| 153 | // permissions or lack thereof. |
| 154 | // |
| 155 | // Example: |
| 156 | // |
| 157 | // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithToken("token")) |
| 158 | // router := http.NewServeMux() |
| 159 | // // Without protection |
| 160 | // router.HandleFunc("/handle", yourHandlerFunc) |
| 161 | // // With protection |
| 162 | // router.HandleFunc("/handle", gate.ProtectFunc(yourHandlerFunc)) |
| 163 | // |
| 164 | // The token extracted from the request is passed to the handlerFunc request context under the key TokenContextKey |
| 165 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) ProtectFuncscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectFunc().(handlerFunc httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/HandlerFunc#) httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/HandlerFunc# { |
| 166 | return gate.ProtectFuncWithPermissionsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectFuncWithPermissions().(handlerFunc, nil) |
| 167 | } |
| 168 | |
| 169 | // ProtectFuncWithPermissions secures a handler, requiring requests going through to have a valid Authorization Bearer |
| 170 | // token as well as a slice of permissions that must be met. |
| 171 | // |
| 172 | // Example: |
| 173 | // |
| 174 | // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClient(g8.NewClient("token").WithPermission("admin"))) |
| 175 | // router := http.NewServeMux() |
| 176 | // // Without protection |
| 177 | // router.HandleFunc("/handle", yourHandlerFunc) |
| 178 | // // With protection |
| 179 | // router.HandleFunc("/handle", gate.ProtectFuncWithPermissions(yourHandlerFunc, []string{"admin"})) |
| 180 | // |
| 181 | // The token extracted from the request is passed to the handlerFunc request context under the key TokenContextKey |
| 182 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) ProtectFuncWithPermissionsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectFuncWithPermissions().(handlerFunc httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/HandlerFunc#, permissions []string) httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/HandlerFunc# { |
| 183 | return func(writer httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/ResponseWriter#, request *httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#) { |
| 184 | if gate.rateLimiterscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#rateLimiter. != nil { |
| 185 | if !gate.rateLimiterscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#rateLimiter..Tryscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#Try().() { |
| 186 | writer.WriteHeaderscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/ResponseWriter#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.StatusTooManyRequestsscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/StatusTooManyRequests.) |
| 187 | _, _ = writer.Writescip-go gomod github.com/golang/go/src go1.23.3 `net/http`/ResponseWriter#Write().(gate.tooManyRequestsResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#tooManyRequestsResponseBody.) |
| 188 | return |
| 189 | } |
| 190 | } |
| 191 | if gate.authorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#authorizationService. != nil { |
| 192 | token := gate.ExtractTokenFromRequestscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ExtractTokenFromRequest().(request) |
| 193 | if client, authorized := gate.authorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#authorizationService..Authorizescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#Authorize().(token, permissions); !authorized { |
| 194 | writer.WriteHeaderscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/ResponseWriter#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.StatusUnauthorizedscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/StatusUnauthorized.) |
| 195 | _, _ = writer.Writescip-go gomod github.com/golang/go/src go1.23.3 `net/http`/ResponseWriter#Write().(gate.unauthorizedResponseBodyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#unauthorizedResponseBody.) |
| 196 | return |
| 197 | } else { |
| 198 | request = request.WithContextscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#WithContext().(contextscip-go gomod github.com/golang/go/src go1.23.3 context/.WithValuescip-go gomod github.com/golang/go/src go1.23.3 context/WithValue().(request.Contextscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#Context().(), TokenContextKeyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/TokenContextKey., token)) |
| 199 | if client != nil && client.Datascip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#Data. != nil { |
| 200 | request = request.WithContextscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#WithContext().(contextscip-go gomod github.com/golang/go/src go1.23.3 context/.WithValuescip-go gomod github.com/golang/go/src go1.23.3 context/WithValue().(request.Contextscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#Context().(), DataContextKeyscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/DataContextKey., client.Datascip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#Data.)) |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | handlerFunc(writer, request) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // ProtectFuncWithPermission does the same thing as ProtectFuncWithPermissions, but for a single permission instead of a |
| 209 | // slice of permissions |
| 210 | // |
| 211 | // See ProtectFuncWithPermissions for further documentation |
| 212 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) ProtectFuncWithPermissionscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectFuncWithPermission().(handlerFunc httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/HandlerFunc#, permission string) httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/HandlerFunc# { |
| 213 | return gate.ProtectFuncWithPermissionsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectFuncWithPermissions().(handlerFunc, []string{permission}) |
| 214 | } |
| 215 | |
| 216 | // ExtractTokenFromRequest extracts a token from a request. |
| 217 | // |
| 218 | // By default, it extracts the bearer token from the AuthorizationHeader, but if a customTokenExtractorFunc is defined, |
| 219 | // it will use that instead. |
| 220 | // |
| 221 | // Note that this method is internally used by Protect, ProtectWithPermission, ProtectFunc and |
| 222 | // ProtectFuncWithPermissions, but it is exposed in case you need to use it directly. |
| 223 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) ExtractTokenFromRequestscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ExtractTokenFromRequest().(request *httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#) string { |
| 224 | if gate.customTokenExtractorFuncscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#customTokenExtractorFunc. != nil { |
| 225 | // A custom token extractor function is defined, so we'll use it instead of the default token extraction logic |
| 226 | return gate.customTokenExtractorFuncscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#customTokenExtractorFunc.(request) |
| 227 | } |
| 228 | return stringsscip-go gomod github.com/golang/go/src go1.23.3 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.23.3 strings/TrimPrefix().(request.Headerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Header#Get().(AuthorizationHeaderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationHeader.), "Bearer ") |
| 229 | } |
| 230 | |
| 231 | // PermissionMiddleware is a middleware that behaves like ProtectWithPermission, but it is meant to be used |
| 232 | // as a middleware for libraries that support such a feature. |
| 233 | // |
| 234 | // For instance, if you are using github.com/gorilla/mux, you can use PermissionMiddleware like so: |
| 235 | // |
| 236 | // router := mux.NewRouter() |
| 237 | // router.Use(gate.PermissionMiddleware("admin")) |
| 238 | // router.Handle("/admin/handle", adminHandler) |
| 239 | // |
| 240 | // If you do not want to protect a router with a specific permission, you can use Gate.Protect instead. |
| 241 | func (gate *Gatescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#) PermissionMiddlewarescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#PermissionMiddleware().(permissions ...string) func(httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler#) httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler# { |
| 242 | return func(next httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler#) httpscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.23.3 `net/http`/Handler# { |
| 243 | return gate.ProtectWithPermissionsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Gate#ProtectWithPermissions().(next, permissions) |
| 244 | } |
| 245 | } |