github.com/in4it/ecs-deploy@v0.0.42-0.20240508120354-ed77ff16df25/provider/ecs/cognito-idp.go (about) 1 package ecs 2 3 import ( 4 "fmt" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/aws/awserr" 8 "github.com/aws/aws-sdk-go/aws/session" 9 "github.com/aws/aws-sdk-go/service/cognitoidentityprovider" 10 "github.com/juju/loggo" 11 ) 12 13 // logging 14 var cognitoLogger = loggo.GetLogger("cognito") 15 16 // Cognito struct 17 type CognitoIdp struct { 18 } 19 20 func (c *CognitoIdp) getUserPoolInfo(userPoolName, userPoolClientName string) (string, string, string, error) { 21 userPoolID, err := c.getUserPoolArn(userPoolName) 22 if err != nil { 23 return "", "", "", err 24 } 25 26 userPool, err := c.describeUserPool(userPoolID) 27 if err != nil { 28 return "", "", "", err 29 } 30 31 userPoolClientID, err := c.getUserPoolClientID(userPoolID, userPoolClientName) 32 if err != nil { 33 return "", "", "", err 34 } 35 36 return aws.StringValue(userPool.Arn), userPoolClientID, aws.StringValue(userPool.Domain), nil 37 } 38 39 func (c *CognitoIdp) describeUserPool(userPoolID string) (*cognitoidentityprovider.UserPoolType, error) { 40 svc := cognitoidentityprovider.New(session.New()) 41 input := &cognitoidentityprovider.DescribeUserPoolInput{ 42 UserPoolId: aws.String(userPoolID), 43 } 44 45 res, err := svc.DescribeUserPool(input) 46 if err != nil { 47 return nil, err 48 } 49 50 return res.UserPool, nil 51 } 52 53 func (c *CognitoIdp) getUserPoolArn(userPoolName string) (string, error) { 54 svc := cognitoidentityprovider.New(session.New()) 55 input := &cognitoidentityprovider.ListUserPoolsInput{ 56 MaxResults: aws.Int64(60), 57 } 58 59 userPoolID := "" 60 61 pageNum := 0 62 err := svc.ListUserPoolsPages(input, 63 func(page *cognitoidentityprovider.ListUserPoolsOutput, lastPage bool) bool { 64 pageNum++ 65 for _, userPool := range page.UserPools { 66 if aws.StringValue(userPool.Name) == userPoolName { 67 userPoolID = aws.StringValue(userPool.Id) 68 } 69 } 70 return pageNum <= 100 71 }) 72 73 if err != nil { 74 if aerr, ok := err.(awserr.Error); ok { 75 cognitoLogger.Errorf(aerr.Error()) 76 } else { 77 cognitoLogger.Errorf(err.Error()) 78 } 79 return userPoolID, err 80 } 81 if userPoolID == "" { 82 return userPoolID, fmt.Errorf("Could not find userpool with name %s", userPoolName) 83 } 84 return userPoolID, nil 85 } 86 func (c *CognitoIdp) getUserPoolClientID(userPoolID, userPoolClientName string) (string, error) { 87 svc := cognitoidentityprovider.New(session.New()) 88 input := &cognitoidentityprovider.ListUserPoolClientsInput{ 89 UserPoolId: aws.String(userPoolID), 90 } 91 92 userPoolClientNameID := "" 93 94 pageNum := 0 95 err := svc.ListUserPoolClientsPages(input, 96 func(page *cognitoidentityprovider.ListUserPoolClientsOutput, lastPage bool) bool { 97 pageNum++ 98 for _, userPoolClient := range page.UserPoolClients { 99 if aws.StringValue(userPoolClient.ClientName) == userPoolClientName { 100 userPoolClientNameID = aws.StringValue(userPoolClient.ClientId) 101 } 102 } 103 return pageNum <= 100 104 }) 105 106 if err != nil { 107 return userPoolClientNameID, err 108 } 109 if userPoolClientNameID == "" { 110 return userPoolClientNameID, fmt.Errorf("Could not find userpool client with name %s", userPoolClientName) 111 } 112 return userPoolClientNameID, nil 113 }