summaryrefslogtreecommitdiff
path: root/client/token.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-02-07 13:48:30 +0100
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2024-02-19 14:15:07 +0100
commit16ac79c9de0dcdc2be7f5bf1c337c514ec2b757c (patch)
tree82693fa6f55168eb3957f3d683009d8195306714 /client/token.go
parent27a3ffe6d065ffe53c11f7629ba29987e39b8aeb (diff)
Client: Refactor to newest internal API
Diffstat (limited to 'client/token.go')
-rw-r--r--client/token.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/client/token.go b/client/token.go
new file mode 100644
index 0000000..d62308b
--- /dev/null
+++ b/client/token.go
@@ -0,0 +1,64 @@
+package client
+
+import (
+ "errors"
+ "fmt"
+
+ srvtypes "github.com/eduvpn/eduvpn-common/types/server"
+ "github.com/jwijenbergh/eduoauth-go"
+)
+
+type cacheMap map[string]eduoauth.Token
+
+type TokenCacher struct {
+ InstituteAccess cacheMap
+ CustomServer cacheMap
+ SecureInternet *eduoauth.Token
+}
+
+func (c *cacheMap) Get(id string) (*eduoauth.Token, error) {
+ if c == nil || len(*c) == 0 {
+ return nil, errors.New("no cache map available")
+ }
+ if v, ok := (*c)[id]; ok {
+ return &v, nil
+ }
+ return nil, fmt.Errorf("identifier: '%s' does not exist in token cache map", id)
+}
+
+func (tc *TokenCacher) Get(id string, t srvtypes.Type) (*eduoauth.Token, error) {
+ switch t {
+ case srvtypes.TypeCustom:
+ return tc.CustomServer.Get(id)
+ case srvtypes.TypeInstituteAccess:
+ return tc.InstituteAccess.Get(id)
+ case srvtypes.TypeSecureInternet:
+ if tc.SecureInternet == nil {
+ return nil, errors.New("no secure internet server available")
+ }
+ return tc.SecureInternet, nil
+ }
+ return nil, fmt.Errorf("invalid type for token cacher get: %d", t)
+}
+
+func (c *cacheMap) Set(id string, t eduoauth.Token) {
+ if c == nil || len(*c) == 0 {
+ *c = make(cacheMap)
+ }
+ (*c)[id] = t
+}
+
+func (tc *TokenCacher) Set(id string, t srvtypes.Type, tok eduoauth.Token) error {
+ switch t {
+ case srvtypes.TypeCustom:
+ tc.CustomServer.Set(id, tok)
+ return nil
+ case srvtypes.TypeInstituteAccess:
+ tc.InstituteAccess.Set(id, tok)
+ return nil
+ case srvtypes.TypeSecureInternet:
+ tc.SecureInternet = &tok
+ return nil
+ }
+ return fmt.Errorf("invalid type for token cacher set: %d", t)
+}