This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
/
auth.go
54 lines (44 loc) · 1.56 KB
/
auth.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
package auth
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
"github.com/gofor-little/xerror"
)
var (
// CognitoClient is the client that interacts with Cognito.
CognitoClient *cognitoidentityprovider.Client
// CognitoUserPoolID is the id of the user pool in Cognito.
CognitoUserPoolID string
// CognitoClientID is the id of the user pool client in Cognito.
CognitoClientID string
)
// Initialize will initialize the auth package. Both profile and region parameters are option if authentication can be
// achieved via another method. For example, environment variables or IAM roles.
func Initialize(ctx context.Context, profile string, region string, cognitoUserPoolID string, cognitoClientID string) error {
var cfg aws.Config
var err error
if profile != "" && region != "" {
cfg, err = config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile(profile), config.WithRegion(region))
} else {
cfg, err = config.LoadDefaultConfig(ctx)
}
if err != nil {
return fmt.Errorf("failed to load default config: %w", err)
}
CognitoClient = cognitoidentityprovider.NewFromConfig(cfg)
CognitoUserPoolID = cognitoUserPoolID
CognitoClientID = cognitoClientID
return nil
}
func checkPackage() error {
if CognitoClient == nil {
return xerror.New("db.CognitoClient is nil, have you called auth.Initialize()?")
}
if CognitoClientID == "" {
return xerror.New("db.CognitoClientID is empty, did you call auth.Initialize()?")
}
return nil
}