/
cognito.go
184 lines (151 loc) · 4.3 KB
/
cognito.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
package internal
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
)
// CognitoClient is a struct that holds the Cognito service client.
type CognitoClient struct {
svc *cognitoidentityprovider.CognitoIdentityProvider
}
// User holds user attributes, username, and status.
type User struct {
Username string
UserAttributes []*cognitoidentityprovider.AttributeType
Status string
}
// NewCognitoClient creates a new CognitoClient instance.
func NewCognitoClient() (*CognitoClient, error) {
awsProfile := GetAWSProfile()
region := GetRegion()
// If the profile is not provided, set it to default
if awsProfile == "" {
awsProfile = "default"
}
var sess *session.Session
var err error
// If the region is provided, create a session with the specified region
if region != "" {
sess, err = session.NewSession(&aws.Config{
Region: aws.String(region),
})
} else {
// Otherwise, use the session with the profile
sess, err = session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: awsProfile,
})
}
if err != nil {
LogError("Error creating session:", err)
return nil, err
}
svc := cognitoidentityprovider.New(sess)
return &CognitoClient{
svc: svc,
}, nil
}
// GetUserByID retrieves user details by their Cognito user ID.
func (c *CognitoClient) GetUserByID(userID string) (*User, error) {
userPoolID := GetPoolID()
input := &cognitoidentityprovider.AdminGetUserInput{
UserPoolId: aws.String(userPoolID),
Username: aws.String(userID),
}
result, err := c.svc.AdminGetUser(input)
if err != nil {
println("getting user:", err)
return nil, err
}
user := &User{
Username: *result.Username,
UserAttributes: result.UserAttributes,
Status: *result.UserStatus,
}
return user, nil
}
// EnableUser enables a user in Amazon Cognito by their Cognito user ID.
func (c *CognitoClient) EnableUser(userID string) error {
userPoolID := GetPoolID()
input := &cognitoidentityprovider.AdminEnableUserInput{
UserPoolId: aws.String(userPoolID),
Username: aws.String(userID),
}
_, err := c.svc.AdminEnableUser(input)
if err != nil {
println("enabling user:", err)
return err
}
return nil
}
// DisableUser disables a user in Amazon Cognito by their Cognito user ID.
func (c *CognitoClient) DisableUser(userID string) error {
userPoolID := GetPoolID()
input := &cognitoidentityprovider.AdminDisableUserInput{
UserPoolId: aws.String(userPoolID),
Username: aws.String(userID),
}
_, err := c.svc.AdminDisableUser(input)
if err != nil {
println("disabling user:", err)
return err
}
return nil
}
// DeleteUser deletes a user from Amazon Cognito by their Cognito user ID.
func (c *CognitoClient) DeleteUser(userID string) error {
userPoolID := GetPoolID()
input := &cognitoidentityprovider.AdminDeleteUserInput{
UserPoolId: aws.String(userPoolID),
Username: aws.String(userID),
}
_, err := c.svc.AdminDeleteUser(input)
if err != nil {
println("deleting user:", err)
return err
}
return nil
}
// ListGroups retrieves a list of all available groups.
func (c *CognitoClient) ListGroups() ([]string, error) {
userPoolID := GetPoolID()
input := &cognitoidentityprovider.ListGroupsInput{
UserPoolId: aws.String(userPoolID),
}
result, err := c.svc.ListGroups(input)
if err != nil {
return nil, err
}
groups := make([]string, len(result.Groups))
for i, group := range result.Groups {
groups[i] = *group.GroupName
}
return groups, nil
}
// CreateGroup creates a new group in the Cognito user pool.
func (c *CognitoClient) CreateGroup(groupName string) error {
userPoolID := GetPoolID()
input := &cognitoidentityprovider.CreateGroupInput{
GroupName: aws.String(groupName),
UserPoolId: aws.String(userPoolID),
Description: aws.String("Description for " + groupName),
}
_, err := c.svc.CreateGroup(input)
if err != nil {
return err
}
return nil
}
// DeleteGroup deletes a group by its name.
func (c *CognitoClient) DeleteGroup(groupName string) error {
userPoolID := GetPoolID()
input := &cognitoidentityprovider.DeleteGroupInput{
GroupName: aws.String(groupName),
UserPoolId: aws.String(userPoolID),
}
_, err := c.svc.DeleteGroup(input)
if err != nil {
return err
}
return nil
}