This repository has been archived by the owner on Oct 26, 2018. It is now read-only.
forked from jitsi/jitsi-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_store.go
103 lines (93 loc) · 2.6 KB
/
token_store.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
97
98
99
100
101
102
103
package jitsi
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
const (
// KeyTeamID is the dynamo key for storing the team id.
// this key is set as the secondary index.
KeyTeamID = "team-id"
// KeyUserID is the dynamo key for storing the user id.
// this is the primary.
KeyUserID = "user-id"
// KeyBotToken is the dynamo key for storing the bot token.
KeyBotToken = "bot-token"
// KeyBotUserID is the dynamo key for storing the bot user id.
KeyBotUserID = "bot-user-id"
// KeyAccessToken is the dynamo ke for storing the access token.
KeyAccessToken = "access-token"
)
// TokenData is the access token data stored from oauth.
type TokenData struct {
TeamID string `json:"team-id"`
UserID string `json:"user-id"`
BotToken string `json:"bot-token"`
BotUserID string `json:"bot-user-id"`
AccessToken string `json:"access-token"`
}
// TokenStore stores and retrieves access tokens from aws dynamodb.
type TokenStore struct {
TableName string
DB *dynamodb.DynamoDB
}
// GetFirstBotTokenForTeam retrieves the first bot token stored with the provided team id.
func (t *TokenStore) GetFirstBotTokenForTeam(teamID string) (string, error) {
teamIDKey := KeyTeamID
queryLimit := int64(1)
queryInput := &dynamodb.QueryInput{
TableName: aws.String(t.TableName),
IndexName: aws.String(fmt.Sprintf("%s-index", KeyTeamID)),
ExpressionAttributeNames: map[string]*string{"#teamid": &teamIDKey},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":v1": {
S: aws.String(teamID),
},
},
KeyConditionExpression: aws.String("#teamid = :v1"),
Limit: &queryLimit,
}
result, err := t.DB.Query(queryInput)
if err != nil {
return "", err
}
if len(result.Items) < 1 {
return "", errors.New(errMissingAuthToken)
}
d := TokenData{}
err = dynamodbattribute.UnmarshalMap(result.Items[0], &d)
if err != nil {
return "", err
}
return d.BotToken, nil
}
// Store will store access token data.
func (t *TokenStore) Store(data *TokenData) error {
input := &dynamodb.PutItemInput{
Item: map[string]*dynamodb.AttributeValue{
KeyTeamID: {
S: aws.String(data.TeamID),
},
KeyUserID: {
S: aws.String(data.UserID),
},
KeyBotToken: {
S: aws.String(data.BotToken),
},
KeyBotUserID: {
S: aws.String(data.BotUserID),
},
KeyAccessToken: {
S: aws.String(data.AccessToken),
},
},
TableName: aws.String(t.TableName),
}
_, err := t.DB.PutItem(input)
if err != nil {
return err
}
return nil
}