This repository has been archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization.go
96 lines (76 loc) · 2.61 KB
/
authorization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package auth
import (
"context"
"fmt"
"strings"
"cloud.google.com/go/datastore"
"github.com/txsvc/commons/pkg/env"
"github.com/txsvc/platform/pkg/platform"
s "github.com/txsvc/platform/pkg/services"
)
const (
// DatastoreAuthorizations collection AUTHORIZATION
DatastoreAuthorizations string = "AUTHORIZATIONS"
// AuthTypeJWT constant jwt
AuthTypeJWT = "jwt"
// AuthTypeSlack constant salack
AuthTypeSlack = "slack"
)
// GetToken returns the oauth token of the workspace integration
func GetToken(ctx context.Context, clientID, authType string) (string, error) {
// ENV always overrides anything else ...
token := env.GetString(strings.ToUpper(fmt.Sprintf("%s_AUTH_TOKEN", authType)), "")
if token != "" {
return token, nil
}
// check the in-memory cache
key := namedKey(clientID, authType)
token, _ = s.GetKV(ctx, key)
if token != "" {
return token, nil
}
auth, err := GetAuthorization(ctx, clientID, authType)
if err != nil {
return "", err
}
// add the token to the cache
s.SetKV(ctx, key, auth.Token, 1800)
return auth.Token, nil
}
// GetAuthorization looks for an authorization
func GetAuthorization(ctx context.Context, clientID, authType string) (*Authorization, error) {
var auth Authorization
k := authorizationKey(clientID, authType)
if err := platform.DataStore().Get(ctx, k, &auth); err != nil {
return nil, err
}
return &auth, nil
}
// FindAuthorization looks for an authorization by token
func FindAuthorization(ctx context.Context, token string) (*Authorization, error) {
var auth []*Authorization
if _, err := platform.DataStore().GetAll(ctx, datastore.NewQuery(DatastoreAuthorizations).Filter("Token =", token), &auth); err != nil {
return nil, err
}
if auth == nil {
return nil, nil
}
return auth[0], nil
}
// CreateAuthorization creates all data needed for the OAuth fu
func CreateAuthorization(ctx context.Context, auth *Authorization) error {
k := authorizationKey(auth.ClientID, auth.AuthType)
// remove the entry from the cache if it is already there ...
s.InvalidateKV(ctx, namedKey(auth.ClientID, auth.AuthType))
// we simply overwrite the existing authorization. If this is no desired, use GetAuthorization first,
// update the Authorization and then write it back.
_, err := platform.DataStore().Put(ctx, k, auth)
return err
}
// authorizationKey creates a datastore key for a workspace authorization based on the team_id.
func authorizationKey(clientID, authType string) *datastore.Key {
return datastore.NameKey(DatastoreAuthorizations, namedKey(clientID, authType), nil)
}
func namedKey(clientID, authType string) string {
return authType + "." + clientID
}