-
Notifications
You must be signed in to change notification settings - Fork 1
/
awssdk.go
141 lines (121 loc) · 5.6 KB
/
awssdk.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
139
140
141
///////////////////////////////////////////////////////////////////
// (c) 2021 Fujitsu Services //
// By: GyanPatel //
// Ref: https://pol-jira.atlassian.net/browse/BMP-4421 //
// Date: 27-Jan-2021 //
// Version: v01.001 //
///////////////////////////////////////////////////////////////////
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
)
const forcePasswordChallengeName = "NEW_PASSWORD_REQUIRED"
// This is the username and password of a user from congnito user pool.
//AuthenticateUser to verify if input user is a valid user
func AuthenticateUser(username string, password string) (string, error) {
log.Println("Info :", "awssdk-AuthenticateUser", "Authentication starting for ", username)
conf := &aws.Config{Region: aws.String("eu-west-2")}
sess := session.Must(session.NewSession(conf))
mac := hmac.New(sha256.New, []byte(secretDetails.CognitoUserPoolClientSecret))
_, err := mac.Write([]byte(username + secretDetails.CognitoUserPoolClientID))
if err != nil {
log.Println("ERROR:AuthenticateUser Error occured awssdk.go - mac.Write ", err)
}
secretHash := base64.StdEncoding.EncodeToString(mac.Sum(nil))
cognitoClient := cognitoidentityprovider.New(sess)
authTry := &cognitoidentityprovider.InitiateAuthInput{
AuthFlow: aws.String(cognitoidentityprovider.AuthFlowTypeUserPasswordAuth),
AuthParameters: map[string]*string{
"USERNAME": aws.String(username),
"PASSWORD": aws.String(password),
"SECRET_HASH": aws.String(secretHash),
},
ClientId: aws.String(secretDetails.CognitoUserPoolClientID),
}
res, err := cognitoClient.InitiateAuth(authTry)
challengeName := aws.StringValue(res.ChallengeName)
AccessToken := ""
if err != nil {
log.Println("ERROR :", "awssdk-AuthenticateUser", username, err)
return "N", err
} else if strings.Compare(challengeName, forcePasswordChallengeName) == 0 {
log.Println("Info :", "awssdk-AuthenticateUser", username, " authenticated - return access R ", err)
return "R", nil
} else if AccessToken = aws.StringValue(res.AuthenticationResult.AccessToken); len(AccessToken) > 0 {
log.Println("Info :", "awssdk-AuthenticateUser", username, " authenticated - return access Y ")
return "Y", nil
} else {
log.Println("Info :", "awssdk-AuthenticateUser", username, " unable to perform authentication ")
return "", nil
}
}
/*
func getAWSSecret(secretName string) (SecretDetails, error) {
log.Println("Info : getAWSSecret: Retrieving application secrets ")
//secretName := secretName
region := "eu-west-2"
secretDetails := SecretDetails{}
//Create a Secrets Manager client
svc := secretsmanager.New(session.New(),
aws.NewConfig().WithRegion(region))
input := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(secretName),
VersionStage: aws.String("AWSCURRENT"), // VersionStage defaults to AWSCURRENT if unspecified
}
// In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
// See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
result, err := svc.GetSecretValue(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case secretsmanager.ErrCodeDecryptionFailure:
// Secrets Manager can't decrypt the protected secret text using the provided KMS key.
log.Println("ERROR : getAWSSecret: ", secretsmanager.ErrCodeDecryptionFailure, aerr.Error())
case secretsmanager.ErrCodeInternalServiceError:
// An error occurred on the server side.
log.Println("ERROR : getAWSSecret: ", secretsmanager.ErrCodeInternalServiceError, aerr.Error())
case secretsmanager.ErrCodeInvalidParameterException:
// You provided an invalid value for a parameter.
log.Println("ERROR : getAWSSecret: ", secretsmanager.ErrCodeInvalidParameterException, aerr.Error())
case secretsmanager.ErrCodeInvalidRequestException:
// You provided a parameter value that is not valid for the current state of the resource.
log.Println("ERROR : getAWSSecret: ", secretsmanager.ErrCodeInvalidRequestException, aerr.Error())
case secretsmanager.ErrCodeResourceNotFoundException:
// We can't find the resource that you asked for.
log.Println("ERROR : getAWSSecret: ", secretsmanager.ErrCodeResourceNotFoundException, aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Println("ERROR : getAWSSecret: ", err.Error())
}
return secretDetails, err
}
// Decrypts secret using the associated KMS CMK.
// Depending on whether the secret is a string or binary, one of these fields will be populated.
var secretString, decodedBinarySecret string
if result.SecretString != nil {
secretString = *result.SecretString
secret := secretString
json.Unmarshal([]byte(secret), &secretDetails)
} else { // This is not needed in if block return the secret
decodedBinarySecretBytes := make([]byte, base64.StdEncoding.DecodedLen(len(result.SecretBinary)))
len, err := base64.StdEncoding.Decode(decodedBinarySecretBytes, result.SecretBinary)
if err != nil {
log.Println("ERROR : getAWSSecret: ", "Base64 Decode Error:", err)
return secretDetails, err
}
decodedBinarySecret = string(decodedBinarySecretBytes[:len])
secret := decodedBinarySecret
json.Unmarshal([]byte(secret), &secretDetails)
}
return secretDetails, nil
}
*/