/
client.go
143 lines (131 loc) · 3.52 KB
/
client.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package dynamodb
import (
"errors"
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"gopkg.in/oauth2.v3"
"gopkg.in/oauth2.v3/models"
)
// ClientConfig client configuration parameters
type ClientConfig struct {
// store clients data collection name(The default is oauth2_clients)
ClientsCName string
}
// ClientStore DynamoDB storage for OAuth 2.0
type ClientStore struct {
ccfg *ClientConfig
dbName string
client *dynamodb.DynamoDB
}
// NewDefaultClientConfig create a default client configuration
func NewDefaultClientConfig() *ClientConfig {
return &ClientConfig{
ClientsCName: "oauth2_clients",
}
}
func initClientTable(client *dynamodb.DynamoDB, clientConfig *ClientConfig) (err error) {
// Create authorization code table
input := &dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String("ID"),
AttributeType: aws.String("S"),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("ID"),
KeyType: aws.String("HASH"),
},
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(10),
WriteCapacityUnits: aws.Int64(10),
},
TableName: &clientConfig.ClientsCName,
}
_, err = client.CreateTable(input)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
switch awsErr.Code() {
case dynamodb.ErrCodeResourceInUseException:
break
default:
fmt.Println("Got error calling CreateTable for clients:")
fmt.Println(awsErr.Error())
os.Exit(1)
}
}
}
return
}
// NewClientStore create a client store instance based on dynamodb
func NewClientStore(client *dynamodb.DynamoDB, ccfg *ClientConfig) (store *ClientStore) {
initClientTable(client, ccfg)
store = &ClientStore{
ccfg: ccfg,
client: client,
}
return
}
// Set set client information
func (cs *ClientStore) Set(info oauth2.ClientInfo) (err error) {
params := &dynamodb.PutItemInput{
TableName: aws.String(cs.ccfg.ClientsCName),
Item: map[string]*dynamodb.AttributeValue{
"ID": &dynamodb.AttributeValue{
S: aws.String(info.GetID()),
},
"Secret": &dynamodb.AttributeValue{
S: aws.String(info.GetSecret()),
},
"Domain": &dynamodb.AttributeValue{
S: aws.String(info.GetDomain()),
},
"UserID": &dynamodb.AttributeValue{
S: aws.String(info.GetUserID()),
},
},
ConditionExpression: aws.String("attribute_not_exists(ID)"),
}
_, err = cs.client.PutItem(params)
return
}
// GetByID according to the ID for the client information
func (cs *ClientStore) GetByID(id string) (info oauth2.ClientInfo, err error) {
input := &dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
"ID": {
S: aws.String(id),
},
},
TableName: aws.String(cs.ccfg.ClientsCName),
}
result, err := cs.client.GetItem(input)
if len(result.Item) == 0 {
err = errors.New("no such client id")
return
}
var infoC models.Client
err = dynamodbattribute.UnmarshalMap(result.Item, &infoC)
info = &infoC
return
}
// RemoveByID use the client id to delete the client information
func (cs *ClientStore) RemoveByID(id string) (err error) {
input := &dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
"ID": {
S: aws.String(id),
},
},
TableName: aws.String(cs.ccfg.ClientsCName),
ConditionExpression: aws.String("attribute_exists(ID)"),
}
_, err = cs.client.DeleteItem(input)
return
}