aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/aws-sdk-go-v2/internal/auth/scheme.go
blob: ff229c048ffce1101d7de8bbbefd79d79a734d4c (plain) (blame)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package auth

import (
	"context"
	"fmt"

	smithy "github.com/aws/smithy-go"
	"github.com/aws/smithy-go/middleware"
)

// SigV4 is a constant representing
// Authentication Scheme Signature Version 4
const SigV4 = "sigv4"

// SigV4A is a constant representing
// Authentication Scheme Signature Version 4A
const SigV4A = "sigv4a"

// None is a constant representing the
// None Authentication Scheme
const None = "none"

// SupportedSchemes is a data structure
// that indicates the list of supported AWS
// authentication schemes
var SupportedSchemes = map[string]bool{
	SigV4:  true,
	SigV4A: true,
	None:   true,
}

// AuthenticationScheme is a representation of
// AWS authentication schemes
type AuthenticationScheme interface {
	isAuthenticationScheme()
}

// AuthenticationSchemeV4 is a AWS SigV4 representation
type AuthenticationSchemeV4 struct {
	Name                  string
	SigningName           *string
	SigningRegion         *string
	DisableDoubleEncoding *bool
}

func (a *AuthenticationSchemeV4) isAuthenticationScheme() {}

// AuthenticationSchemeV4A is a AWS SigV4A representation
type AuthenticationSchemeV4A struct {
	Name                  string
	SigningName           *string
	SigningRegionSet      []string
	DisableDoubleEncoding *bool
}

func (a *AuthenticationSchemeV4A) isAuthenticationScheme() {}

// AuthenticationSchemeNone is a representation for the none auth scheme
type AuthenticationSchemeNone struct{}

func (a *AuthenticationSchemeNone) isAuthenticationScheme() {}

// NoAuthenticationSchemesFoundError is used in signaling
// that no authentication schemes have been specified.
type NoAuthenticationSchemesFoundError struct{}

func (e *NoAuthenticationSchemesFoundError) Error() string {
	return fmt.Sprint("No authentication schemes specified.")
}

// UnSupportedAuthenticationSchemeSpecifiedError is used in
// signaling that only unsupported authentication schemes
// were specified.
type UnSupportedAuthenticationSchemeSpecifiedError struct {
	UnsupportedSchemes []string
}

func (e *UnSupportedAuthenticationSchemeSpecifiedError) Error() string {
	return fmt.Sprint("Unsupported authentication scheme specified.")
}

// GetAuthenticationSchemes extracts the relevant authentication scheme data
// into a custom strongly typed Go data structure.
func GetAuthenticationSchemes(p *smithy.Properties) ([]AuthenticationScheme, error) {
	var result []AuthenticationScheme
	if !p.Has("authSchemes") {
		return nil, &NoAuthenticationSchemesFoundError{}
	}

	authSchemes, _ := p.Get("authSchemes").([]interface{})

	var unsupportedSchemes []string
	for _, scheme := range authSchemes {
		authScheme, _ := scheme.(map[string]interface{})

		switch authScheme["name"] {
		case SigV4:
			v4Scheme := AuthenticationSchemeV4{
				Name:                  SigV4,
				SigningName:           getSigningName(authScheme),
				SigningRegion:         getSigningRegion(authScheme),
				DisableDoubleEncoding: getDisableDoubleEncoding(authScheme),
			}
			result = append(result, AuthenticationScheme(&v4Scheme))
		case SigV4A:
			v4aScheme := AuthenticationSchemeV4A{
				Name:                  SigV4A,
				SigningName:           getSigningName(authScheme),
				SigningRegionSet:      getSigningRegionSet(authScheme),
				DisableDoubleEncoding: getDisableDoubleEncoding(authScheme),
			}
			result = append(result, AuthenticationScheme(&v4aScheme))
		case None:
			noneScheme := AuthenticationSchemeNone{}
			result = append(result, AuthenticationScheme(&noneScheme))
		default:
			unsupportedSchemes = append(unsupportedSchemes, authScheme["name"].(string))
			continue
		}
	}

	if len(result) == 0 {
		return nil, &UnSupportedAuthenticationSchemeSpecifiedError{
			UnsupportedSchemes: unsupportedSchemes,
		}
	}

	return result, nil
}

type disableDoubleEncoding struct{}

// SetDisableDoubleEncoding sets or modifies the disable double encoding option
// on the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func SetDisableDoubleEncoding(ctx context.Context, value bool) context.Context {
	return middleware.WithStackValue(ctx, disableDoubleEncoding{}, value)
}

// GetDisableDoubleEncoding retrieves the disable double encoding option
// from the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func GetDisableDoubleEncoding(ctx context.Context) (value bool, ok bool) {
	value, ok = middleware.GetStackValue(ctx, disableDoubleEncoding{}).(bool)
	return value, ok
}

func getSigningName(authScheme map[string]interface{}) *string {
	signingName, ok := authScheme["signingName"].(string)
	if !ok || signingName == "" {
		return nil
	}
	return &signingName
}

func getSigningRegionSet(authScheme map[string]interface{}) []string {
	untypedSigningRegionSet, ok := authScheme["signingRegionSet"].([]interface{})
	if !ok {
		return nil
	}
	signingRegionSet := []string{}
	for _, item := range untypedSigningRegionSet {
		signingRegionSet = append(signingRegionSet, item.(string))
	}
	return signingRegionSet
}

func getSigningRegion(authScheme map[string]interface{}) *string {
	signingRegion, ok := authScheme["signingRegion"].(string)
	if !ok || signingRegion == "" {
		return nil
	}
	return &signingRegion
}

func getDisableDoubleEncoding(authScheme map[string]interface{}) *bool {
	disableDoubleEncoding, ok := authScheme["disableDoubleEncoding"].(bool)
	if !ok {
		return nil
	}
	return &disableDoubleEncoding
}