Debian Codegraph @

authorization.go

raw symbols SCIP
1 package g8scip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/
2
3 import (
4 "syncscip-go gomod github.com/golang/go/src go1.23.3 sync/"
5 )
6
7 // AuthorizationService is the service that manages client/token registry and client fallback as well as the service
8 // that determines whether a token meets the specific requirements to be authorized by a Gate or not.
9 type AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService# struct {
10 clientsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clients. map[string]*Clientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#
11 clientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clientProvider. *ClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#
12
13 mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex. syncscip-go gomod github.com/golang/go/src go1.23.3 sync/.RWMutexscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#
14 }
15
16 // NewAuthorizationService creates a new AuthorizationService
17 func NewAuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/NewAuthorizationService().() *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService# {
18 return &AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#{
19 clientsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clients.: make(map[string]*Clientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#),
20 }
21 }
22
23 // WithToken is used to specify a single token for which authorization will be granted
24 //
25 // The client that will be created from this token will have access to all handlers that are not protected with a
26 // specific permission.
27 //
28 // In other words, if you were to do the following:
29 //
30 // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithToken("12345"))
31 //
32 // The following handler would be accessible with the token 12345:
33 //
34 // router.Handle("/1st-handler", gate.Protect(yourHandler))
35 //
36 // But not this one would not be accessible with the token 12345:
37 //
38 // router.Handle("/2nd-handler", gate.ProtectWithPermissions(yourOtherHandler, []string{"admin"}))
39 //
40 // Calling this function multiple times will add multiple clients, though you may want to use WithTokens instead
41 // if you plan to add multiple clients
42 //
43 // If you wish to configure advanced permissions, consider using WithClient instead.
44 func (authorizationService *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#) WithTokenscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#WithToken().(token string) *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService# {
45 authorizationService.mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex..Lockscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#Lock().()
46 authorizationService.clientsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clients.[token] = NewClientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/NewClient().(token)
47 authorizationService.mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex..Unlockscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#Unlock().()
48 return authorizationService
49 }
50
51 // WithTokens is used to specify a slice of tokens for which authorization will be granted
52 func (authorizationService *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#) WithTokensscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#WithTokens().(tokens []string) *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService# {
53 authorizationService.mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex..Lockscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#Lock().()
54 for _, token := range tokens {
55 authorizationService.clientsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clients.[token] = NewClientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/NewClient().(token)
56 }
57 authorizationService.mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex..Unlockscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#Unlock().()
58 return authorizationService
59 }
60
61 // WithClient is used to specify a single client for which authorization will be granted
62 //
63 // When compared to WithToken, the advantage of using this function is that you may specify the client's
64 // permissions and thus, be a lot more granular with what endpoint a token has access to.
65 //
66 // In other words, if you were to do the following:
67 //
68 // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClient(g8.NewClient("12345").WithPermission("mod")))
69 //
70 // The following handlers would be accessible with the token 12345:
71 //
72 // router.Handle("/1st-handler", gate.ProtectWithPermissions(yourHandler, []string{"mod"}))
73 // router.Handle("/2nd-handler", gate.Protect(yourOtherHandler))
74 //
75 // But not this one, because the user does not have the permission "admin":
76 //
77 // router.Handle("/3rd-handler", gate.ProtectWithPermissions(yetAnotherHandler, []string{"admin"}))
78 //
79 // Calling this function multiple times will add multiple clients, though you may want to use WithClients instead
80 // if you plan to add multiple clients
81 func (authorizationService *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#) WithClientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#WithClient().(client *Clientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#) *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService# {
82 authorizationService.mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex..Lockscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#Lock().()
83 authorizationService.clientsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clients.[client.Tokenscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#Token.] = client
84 authorizationService.mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex..Unlockscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#Unlock().()
85 return authorizationService
86 }
87
88 // WithClients is used to specify a slice of clients for which authorization will be granted
89 func (authorizationService *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#) WithClientsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#WithClients().(clients []*Clientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#) *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService# {
90 authorizationService.mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex..Lockscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#Lock().()
91 for _, client := range clients {
92 authorizationService.clientsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clients.[client.Tokenscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#Token.] = client
93 }
94 authorizationService.mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex..Unlockscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#Unlock().()
95 return authorizationService
96 }
97
98 // WithClientProvider allows specifying a custom provider to fetch clients by token.
99 //
100 // For example, you can use it to fallback to making a call in your database when a request is made with a token that
101 // hasn't been specified via WithToken, WithTokens, WithClient or WithClients.
102 func (authorizationService *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#) WithClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#WithClientProvider().(provider *ClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#) *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService# {
103 authorizationService.clientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clientProvider. = provider
104 return authorizationService
105 }
106
107 // Authorize checks whether a client with a given token exists and has the permissions required.
108 //
109 // If permissionsRequired is nil or empty and a client with the given token exists, said client will have access to all
110 // handlers that are not protected by a given permission.
111 //
112 // Returns the client is authorized (or nil if no client was authorized), as well as whether the token is authorized
113 func (authorizationService *AuthorizationServicescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#) Authorizescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#Authorize().(token string, permissionsRequired []string) (client *Clientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#, authorized bool) {
114 if len(token) == 0 {
115 return nil, false
116 }
117 authorizationService.mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex..RLockscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#RLock().()
118 client, _ = authorizationService.clientsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clients.[token]
119 authorizationService.mutexscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#mutex..RUnlockscip-go gomod github.com/golang/go/src go1.23.3 sync/RWMutex#RUnlock().()
120 // If there's no clients with the given token directly stored in the AuthorizationService, fall back to the
121 // client provider, if there's one configured.
122 if client == nil && authorizationService.clientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clientProvider. != nil {
123 client = authorizationService.clientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/AuthorizationService#clientProvider..GetClientByTokenscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#GetClientByToken().(token)
124 }
125 if client != nil && client.HasPermissionsscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#HasPermissions().(permissionsRequired) {
126 // If the client has the required permissions, return true and the client
127 return client, true
128 }
129 return nil, false
130 }