-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (40 loc) 路 1.32 KB
/
main.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
package main
import (
"context"
"fmt"
"net/http"
"github.com/klemenkozelj/go-aws-sts-authenticator/client"
"github.com/klemenkozelj/go-aws-sts-authenticator/server"
)
func main() {
xAmzDate, authorization, xAmzSecurityToken, awsRegion := clientExample()
serverExample(xAmzDate, authorization, xAmzSecurityToken, awsRegion)
mux := http.NewServeMux()
awsAuth := server.AuthenticateAwsIamIdentity(
server.DefaultGetRequestParameters,
server.DefaultIsIamIdentityValid("arn:aws:iam::1234567890:user/username"),
)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Your handler is running!")
})
mux.Handle("/", awsAuth(handler))
}
func clientExample() (string, string, string, string) {
ctx := context.Background()
signatureClient, err := client.NewClient(ctx)
if err != nil {
panic(err)
}
xAmzDate, authorization, xAmzSecurityToken, awsRegion, err := signatureClient.GetStsParameters(ctx)
if err != nil {
panic(err)
}
return xAmzDate, authorization, xAmzSecurityToken, awsRegion
}
func serverExample(date, authorization, token, region string) {
identity, err := server.StsGetCallerIdentity("https://sts."+region+".amazonaws.com", date, authorization, token)
if err != nil {
panic(err)
}
fmt.Println("AWS IAM ARN = ", identity.GetCallerIdentityResult.Arn)
}