-
Notifications
You must be signed in to change notification settings - Fork 9
/
session.go
212 lines (182 loc) · 5.16 KB
/
session.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package dynamodb
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/madappgang/identifo/v2/model"
)
const (
adminSessionsTableName = "AdminSessions"
)
// DynamoDBSessionStorage is a DynamoDB-backed storage for admin sessions.
type DynamoDBSessionStorage struct {
db *dynamodb.DynamoDB
}
// NewSessionStorage creates new DynamoDB session storage.
func NewSessionStorage(settings model.DynamoDatabaseSettings) (model.SessionStorage, error) {
config := &aws.Config{
Region: aws.String(settings.Region),
Endpoint: aws.String(settings.Endpoint),
}
sess, err := session.NewSession(config)
if err != nil {
log.Println(err)
return nil, err
}
dss := &DynamoDBSessionStorage{db: dynamodb.New(sess)}
err = dss.ensureTable()
return dss, err
}
// GetSession fetches session by ID.
func (dss *DynamoDBSessionStorage) GetSession(id string) (model.Session, error) {
var session model.Session
if len(id) == 0 {
return session, model.ErrSessionNotFound
}
getItemInput := &dynamodb.GetItemInput{
TableName: aws.String(adminSessionsTableName),
Key: map[string]*dynamodb.AttributeValue{
"id": {
S: aws.String(id),
},
},
}
result, err := dss.db.GetItem(getItemInput)
if err != nil {
return session, err
}
if result.Item == nil {
return session, model.ErrSessionNotFound
}
if err = dynamodbattribute.UnmarshalMap(result.Item, &session); err != nil {
return session, fmt.Errorf("Error unmarshaling item: %s", err)
}
return session, nil
}
// InsertSession inserts session to the storage.
func (dss *DynamoDBSessionStorage) InsertSession(session model.Session) error {
sessMap, err := dynamodbattribute.MarshalMap(session)
if err != nil {
return fmt.Errorf("Error marshalling session: %s", err)
}
input := &dynamodb.PutItemInput{
TableName: aws.String(adminSessionsTableName),
Item: sessMap,
}
if _, err = dss.db.PutItem(input); err != nil {
return fmt.Errorf("Error putting session to the storage: %s", err)
}
return err
}
// DeleteSession deletes session from the storage.
func (dss *DynamoDBSessionStorage) DeleteSession(id string) error {
if len(id) == 0 {
return nil
}
input := &dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
"id": {
S: aws.String(id),
},
},
TableName: aws.String(adminSessionsTableName),
}
_, err := dss.db.DeleteItem(input)
return err
}
// ProlongSession sets new duration for the existing session.
func (dss *DynamoDBSessionStorage) ProlongSession(id string, newDuration model.SessionDuration) error {
session, err := dss.GetSession(id)
if err != nil {
return err
}
session.ExpirationTime = time.Now().Add(newDuration.Duration).Unix()
return dss.InsertSession(session)
}
// ensureTable ensures that admin sessions table exists in database.
func (dss *DynamoDBSessionStorage) ensureTable() error {
exists, err := dss.isTableExists(adminSessionsTableName)
if err != nil {
log.Println("Error checking admins sessions table existence:", err)
return err
}
if exists {
return nil
}
input := &dynamodb.CreateTableInput{
TableName: aws.String(adminSessionsTableName),
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String("id"),
AttributeType: aws.String("S"),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("id"),
KeyType: aws.String("HASH"),
},
},
BillingMode: aws.String("PAY_PER_REQUEST"),
}
_, err = dss.db.CreateTable(input)
if err != nil {
return fmt.Errorf("Cannot create admin sessions table: %s", err)
}
ttlSpecification := &dynamodb.TimeToLiveSpecification{
AttributeName: aws.String("expiration_time"),
Enabled: aws.Bool(true),
}
ttlInput := &dynamodb.UpdateTimeToLiveInput{
TableName: aws.String(adminSessionsTableName),
TimeToLiveSpecification: ttlSpecification,
}
if _, err = dss.db.UpdateTimeToLive(ttlInput); err != nil {
if err.Error() == dynamodb.ErrCodeResourceNotFoundException {
// Then table must be in creating status. Let's give it some time.
for i := 0; i < 5; i++ {
time.Sleep(5 * time.Second)
log.Println("Retry setting expiration time...")
if _, err = dss.db.UpdateTimeToLive(ttlInput); err == nil {
log.Println("Expiration time successfully set")
break
}
}
}
}
return err
}
// isTableExists checks if table exists.
func (dss *DynamoDBSessionStorage) isTableExists(table string) (bool, error) {
input := &dynamodb.DescribeTableInput{
TableName: aws.String(table),
}
_, err := dss.db.DescribeTable(input)
if awsErrorNotFound(err) {
return false, nil
}
if err != nil {
log.Println(err)
return false, err
}
return true, nil
}
// awsErrorNotFound checks if error has type dynamodb.ErrCodeResourceNotFoundException.
func awsErrorNotFound(err error) bool {
if err == nil {
return false
}
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() == dynamodb.ErrCodeResourceNotFoundException {
return true
}
}
return false
}
func (is *DynamoDBSessionStorage) Close() {
}