-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetToken.go
78 lines (69 loc) · 1.98 KB
/
getToken.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/joho/godotenv"
"os"
"log"
)
func main() {
loadEnv()
username := os.Getenv("COGNITO_USER_NAME")
password := os.Getenv("COGNITO_PASSWORD")
clientId := os.Getenv("COGNITO_CLIENT_ID")
userPoolId := os.Getenv("COGNITO_USER_POOL_ID")
// newPassword := os.Getenv("COGNITO_NEW_PASSWORD")
svc := cognitoidentityprovider.New(session.New(), &aws.Config{Region: aws.String("us-west-2")})
// ログイン
params := &cognitoidentityprovider.AdminInitiateAuthInput{
AuthFlow: aws.String("ADMIN_NO_SRP_AUTH"),
AuthParameters: map[string]*string{
"USERNAME": aws.String(username),
"PASSWORD": aws.String(password),
},
ClientId: aws.String(clientId),
UserPoolId: aws.String(userPoolId),
}
resp, err := svc.AdminInitiateAuth(params)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(resp)
// 初期パスワード変更
// session := resp.Session
// r_params := &cognitoidentityprovider.AdminRespondToAuthChallengeInput{
// ChallengeName: aws.String("NEW_PASSWORD_REQUIRED"),
// ChallengeResponses: map[string]*string{
// "NEW_PASSWORD": aws.String(newPassword),
// "USERNAME": aws.String(username),
// },
// ClientId: aws.String(clientId),
// Session: session,
// UserPoolId: aws.String(userPoolId),
// }
// r_resp, err := svc.AdminRespondToAuthChallenge(r_params)
// if err != nil {
// fmt.Println(err.Error())
// return
// }
// fmt.Println(r_resp)
// // ログアウト by AccessToken
// o_params := &cognitoidentityprovider.GlobalSignOutInput{
// AccessToken: aws.String(*resp.AuthenticationResult.AccessToken),
// }
// o_resp, err := svc.GlobalSignOut(o_params)
// if err != nil {
// fmt.Println(err.Error())
// return
// }
// fmt.Println(o_resp)
}
func loadEnv() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading %v\n", err)
}
}