-
Notifications
You must be signed in to change notification settings - Fork 2
/
login.go
55 lines (45 loc) · 1.51 KB
/
login.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
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
)
// Cognito API calls. You should store this information
// in other way, because this way it would be possible
// to extract those strings from your compiled app if it
// leaks somewhere.
const clientId = ""
const clientSecret = ""
// This is the username and password of a user from your
// Cognito user pool.
const username = ""
const password = ""
func main() {
conf := &aws.Config{Region: aws.String("us-east-1")}
sess := session.Must(session.NewSession(conf))
// This is the part where we generate the hash.
mac := hmac.New(sha256.New, []byte(clientSecret))
mac.Write([]byte(username + clientId))
secretHash := base64.StdEncoding.EncodeToString(mac.Sum(nil))
cognitoClient := cognitoidentityprovider.New(sess)
authTry := &cognitoidentityprovider.InitiateAuthInput{
AuthFlow: aws.String("USER_PASSWORD_AUTH"),
AuthParameters: map[string]*string{
"USERNAME": aws.String(username),
"PASSWORD": aws.String(password),
"SECRET_HASH": aws.String(secretHash),
},
ClientId: aws.String(clientId),
}
res, err := cognitoClient.InitiateAuth(authTry)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("authenticated")
fmt.Println(res.AuthenticationResult)
}
}