forked from tmieulet/xk6-cognito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xk6-cognito.go
138 lines (101 loc) · 3.35 KB
/
xk6-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
package cognito
import (
"context"
"fmt"
"time"
cognitosrp "github.com/alexrudd/cognito-srp/v4"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
cip "github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider/types"
// "go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
)
// Register the extension on module initialization, available to
// import from JS as "k6/x/cognito".
func init() {
modules.Register("k6/x/cognito", new(Cognito))
}
// Cognito is the k6 extension for a Cognito client.
type Cognito struct{}
// Client is the Cognito client wrapper.
type Client struct {
ctx context.Context
// https://github.com/aws/aws-sdk-go-v2/blob/main/service/cognitoidentityprovider/api_client.go
client *cip.Client
}
type keyValue map[string]interface{}
type AuthOptionalParams struct {
// https://stackoverflow.com/questions/2032149/optional-parameters-in-go
clientMetadata map[string]string
cognitoSecret *string
}
func contains(array []string, element string) bool {
for _, item := range array {
if item == element {
return true
}
}
return false
}
func (r *Cognito) Connect(region string ) (*Client, error) {
regionAws := config.WithRegion(region)
// cred := config.WithCredentialsProvider(aws.AnonymousCredentials{})
// configure cognito identity provider
// https://github.com/aws/aws-sdk-go-v2
cfg, err := config.LoadDefaultConfig(
context.TODO(),
regionAws ,
)
if err != nil {
return nil, err
}
client := Client{
ctx: context.TODO(),
client: cip.NewFromConfig(cfg),
}
return &client, nil
}
func (c *Client) Auth( username string, password string, poolId string, clientId string, params AuthOptionalParams ) (keyValue, error) {
// configure cognito srp
// https://github.com/alexrudd/cognito-srp
csrp, _ := cognitosrp.NewCognitoSRP(username, password, poolId, clientId, params.cognitoSecret)
// initiate auth
resp, err := c.client.InitiateAuth(c.ctx, &cip.InitiateAuthInput{
AuthFlow: types.AuthFlowTypeUserSrpAuth,
ClientId: aws.String(csrp.GetClientId()),
AuthParameters: csrp.GetAuthParams(),
ClientMetadata: params.clientMetadata,
})
if err != nil {
return nil, err
}
// respond to password verifier challenge
if resp.ChallengeName == types.ChallengeNameTypePasswordVerifier {
challengeResponses, err := csrp.PasswordVerifierChallenge(resp.ChallengeParameters, time.Now())
if err != nil {
return nil, err
}
resp, err := c.client.RespondToAuthChallenge(c.ctx, &cip.RespondToAuthChallengeInput{
ChallengeName: types.ChallengeNameTypePasswordVerifier,
ChallengeResponses: challengeResponses,
ClientId: aws.String(csrp.GetClientId()),
})
if err != nil {
return nil, err
}
// data := make(keyValue, 3)
data := keyValue{
"AccessToken": *resp.AuthenticationResult.AccessToken,
"IdToken": *resp.AuthenticationResult.IdToken ,
"RefreshToken" : *resp.AuthenticationResult.RefreshToken,
}
// data["AccessToken"] := *resp.AuthenticationResult.AccessToken
// data["IdToken"] := *resp.AuthenticationResult.IdToken
// data["RefreshToken"] := *resp.AuthenticationResult.RefreshToken
return data, nil
} else {
// other challenges await...
return nil, fmt.Errorf("Challenge %s is not supported", resp.ChallengeName)
}
}