Debian Codegraph @

clientprovider.go

raw symbols SCIP
1 package g8
2
3 import (
4 "time"
5
6 "github.com/TwiN/gocache/v2"
7 )
8
9 // ClientProvider has the task of retrieving a Client from an external source (e.g. a database) when provided with a
10 // token. It should be used when you have a lot of tokens, and it wouldn't make sense to register all of them using
11 // AuthorizationService's WithToken, WithTokens, WithClient or WithClients.
12 //
13 // Note that the provider is used as a fallback source. As such, if a token is explicitly registered using one of the 4
14 // aforementioned functions, the client provider will not be used by the AuthorizationService when a request is made
15 // with said token. It will, however, be called upon if a token that is not explicitly registered in
16 // AuthorizationService is sent alongside a request going through the Gate.
17 //
18 // clientProvider := g8.NewClientProvider(func(token string) *g8.Client {
19 // // We'll assume that the following function calls your database and returns a struct "User" that
20 // // has the user's token as well as the permissions granted to said user
21 // user := database.GetUserByToken(token)
22 // if user != nil {
23 // return g8.NewClient(user.Token).WithPermissions(user.Permissions)
24 // }
25 // return nil
26 // })
27 // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClientProvider(clientProvider))
28 type ClientProvider struct {
29 getClientByTokenFunc func(token string) *Client
30
31 cache Cache
32 }
33
34 // NewClientProvider creates a ClientProvider
35 // The parameter that must be passed is a function that the provider will use to retrieve a client by a given token
36 //
37 // Example:
38 //
39 // clientProvider := g8.NewClientProvider(func(token string) *g8.Client {
40 // // We'll assume that the following function calls your database and returns a struct "User" that
41 // // has the user's token as well as the permissions granted to said user
42 // user := database.GetUserByToken(token)
43 // if user == nil {
44 // return nil
45 // }
46 // return g8.NewClient(user.Token).WithPermissions(user.Permissions)
47 // })
48 // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClientProvider(clientProvider))
49 func NewClientProvider(getClientByTokenFunc func(token string) *Client) *ClientProvider {
50 return &ClientProvider{
51 getClientByTokenFunc: getClientByTokenFunc,
52 }
53 }
54
55 // WithCache enables an in-memory cache for the ClientProvider.
56 //
57 // Example:
58 //
59 // clientProvider := g8.NewClientProvider(func(token string) *g8.Client {
60 // // We'll assume that the following function calls your database and returns a struct "User" that
61 // // has the user's token as well as the permissions granted to said user
62 // user := database.GetUserByToken(token)
63 // if user != nil {
64 // return g8.NewClient(user.Token).WithPermissions(user.Permissions)
65 // }
66 // return nil
67 // })
68 // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClientProvider(clientProvider.WithCache(time.Hour, 70000)))
69 func (provider *ClientProvider) WithCache(ttl time.Duration, maxSize int) *ClientProvider {
70 return provider.WithCustomCache(
71 gocache.NewCache().WithEvictionPolicy(gocache.LeastRecentlyUsed).WithMaxSize(maxSize).WithDefaultTTL(ttl),
72 )
73 }
74
75 // WithCustomCache allows you to use a custom cache implementation instead of the default one.
76 // By default, using WithCache will leverage gocache.
77 //
78 // Note that the custom cache must implement the Cache interface
79 func (provider *ClientProvider) WithCustomCache(cache Cache) *ClientProvider {
80 provider.cache = cache
81 return provider
82 }
83
84 // GetClientByToken retrieves a client by its token through the provided getClientByTokenFunc.
85 func (provider *ClientProvider) GetClientByToken(token string) *Client {
86 if provider.cache == nil {
87 return provider.getClientByTokenFunc(token)
88 }
89 if cachedClient, exists := provider.cache.Get(token); exists {
90 if cachedClient == nil {
91 return nil
92 }
93 // Safely typecast the client.
94 // Regardless of whether the typecast is successful or not, we return client since it'll be either client or
95 // nil. Technically, it should never be nil, but it's better to be safe than sorry.
96 client, _ := cachedClient.(*Client)
97 return client
98 }
99 client := provider.getClientByTokenFunc(token)
100 provider.cache.Set(token, client)
101 return client
102 }