| 1 | package g8 |
| 2 | |
| 3 | import ( |
| 4 | "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 AuthorizationService struct { |
| 10 | clients map[string]*Client |
| 11 | clientProvider *ClientProvider |
| 12 | |
| 13 | mutex sync.RWMutex |
| 14 | } |
| 15 | |
| 16 | // NewAuthorizationService creates a new AuthorizationService |
| 17 | func NewAuthorizationService() *AuthorizationService { |
| 18 | return &AuthorizationService{ |
| 19 | clients: make(map[string]*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 *AuthorizationService) WithToken(token string) *AuthorizationService { |
| 45 | authorizationService.mutex.Lock() |
| 46 | authorizationService.clients[token] = NewClient(token) |
| 47 | authorizationService.mutex.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 *AuthorizationService) WithTokens(tokens []string) *AuthorizationService { |
| 53 | authorizationService.mutex.Lock() |
| 54 | for _, token := range tokens { |
| 55 | authorizationService.clients[token] = NewClient(token) |
| 56 | } |
| 57 | authorizationService.mutex.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 *AuthorizationService) WithClient(client *Client) *AuthorizationService { |
| 82 | authorizationService.mutex.Lock() |
| 83 | authorizationService.clients[client.Token] = client |
| 84 | authorizationService.mutex.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 *AuthorizationService) WithClients(clients []*Client) *AuthorizationService { |
| 90 | authorizationService.mutex.Lock() |
| 91 | for _, client := range clients { |
| 92 | authorizationService.clients[client.Token] = client |
| 93 | } |
| 94 | authorizationService.mutex.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 *AuthorizationService) WithClientProvider(provider *ClientProvider) *AuthorizationService { |
| 103 | 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 *AuthorizationService) Authorize(token string, permissionsRequired []string) (client *Client, authorized bool) { |
| 114 | if len(token) == 0 { |
| 115 | return nil, false |
| 116 | } |
| 117 | authorizationService.mutex.RLock() |
| 118 | client, _ = authorizationService.clients[token] |
| 119 | authorizationService.mutex.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.clientProvider != nil { |
| 123 | client = authorizationService.clientProvider.GetClientByToken(token) |
| 124 | } |
| 125 | if client != nil && 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 | } |