/
cognito-idp.go
113 lines (95 loc) · 2.96 KB
/
cognito-idp.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
package ecs
import (
"fmt"
"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/cognitoidentityprovider"
"github.com/juju/loggo"
)
// logging
var cognitoLogger = loggo.GetLogger("cognito")
// Cognito struct
type CognitoIdp struct {
}
func (c *CognitoIdp) getUserPoolInfo(userPoolName, userPoolClientName string) (string, string, string, error) {
userPoolID, err := c.getUserPoolArn(userPoolName)
if err != nil {
return "", "", "", err
}
userPool, err := c.describeUserPool(userPoolID)
if err != nil {
return "", "", "", err
}
userPoolClientID, err := c.getUserPoolClientID(userPoolID, userPoolClientName)
if err != nil {
return "", "", "", err
}
return aws.StringValue(userPool.Arn), userPoolClientID, aws.StringValue(userPool.Domain), nil
}
func (c *CognitoIdp) describeUserPool(userPoolID string) (*cognitoidentityprovider.UserPoolType, error) {
svc := cognitoidentityprovider.New(session.New())
input := &cognitoidentityprovider.DescribeUserPoolInput{
UserPoolId: aws.String(userPoolID),
}
res, err := svc.DescribeUserPool(input)
if err != nil {
return nil, err
}
return res.UserPool, nil
}
func (c *CognitoIdp) getUserPoolArn(userPoolName string) (string, error) {
svc := cognitoidentityprovider.New(session.New())
input := &cognitoidentityprovider.ListUserPoolsInput{
MaxResults: aws.Int64(60),
}
userPoolID := ""
pageNum := 0
err := svc.ListUserPoolsPages(input,
func(page *cognitoidentityprovider.ListUserPoolsOutput, lastPage bool) bool {
pageNum++
for _, userPool := range page.UserPools {
if aws.StringValue(userPool.Name) == userPoolName {
userPoolID = aws.StringValue(userPool.Id)
}
}
return pageNum <= 100
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
cognitoLogger.Errorf(aerr.Error())
} else {
cognitoLogger.Errorf(err.Error())
}
return userPoolID, err
}
if userPoolID == "" {
return userPoolID, fmt.Errorf("Could not find userpool with name %s", userPoolName)
}
return userPoolID, nil
}
func (c *CognitoIdp) getUserPoolClientID(userPoolID, userPoolClientName string) (string, error) {
svc := cognitoidentityprovider.New(session.New())
input := &cognitoidentityprovider.ListUserPoolClientsInput{
UserPoolId: aws.String(userPoolID),
}
userPoolClientNameID := ""
pageNum := 0
err := svc.ListUserPoolClientsPages(input,
func(page *cognitoidentityprovider.ListUserPoolClientsOutput, lastPage bool) bool {
pageNum++
for _, userPoolClient := range page.UserPoolClients {
if aws.StringValue(userPoolClient.ClientName) == userPoolClientName {
userPoolClientNameID = aws.StringValue(userPoolClient.ClientId)
}
}
return pageNum <= 100
})
if err != nil {
return userPoolClientNameID, err
}
if userPoolClientNameID == "" {
return userPoolClientNameID, fmt.Errorf("Could not find userpool client with name %s", userPoolClientName)
}
return userPoolClientNameID, nil
}