-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization.go
100 lines (78 loc) · 2.69 KB
/
authorization.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
package go_requests
type Authorization interface {
// Basic sets the authorization to basic
Bearer(token string) Authorization
// Basic sets the authorization to basic
Basic(username, password string) Authorization
// String returns the string representation of the authorization
String() string
// Type returns the type of the authorization
Type() AuthorizationType
// Value returns the value of the authorization
Value() string
// IsBasic returns true if the authorization is basic
IsBasic() bool
// IsBearer returns true if the authorization is bearer
IsBearer() bool
// IsEmpty returns true if the authorization is empty
IsEmpty() bool
// IsSet returns true if the authorization is set
IsSet() bool
}
// AuthorizationType is the type of the authorization header value
type AuthorizationType string
const (
//AuthorizationTypeBasic is the basic authorization type
AuthorizationTypeBasic AuthorizationType = "Basic"
//AuthorizationTypeBearer is the bearer authorization type
AuthorizationTypeBearer AuthorizationType = "Bearer"
)
// authorizationImpl is the implementation of the authorization interface
type authorizationImpl struct {
authorizationType AuthorizationType
value string
}
// Bearer sets the authorization to bearer
func (a *authorizationImpl) Bearer(token string) Authorization {
a.authorizationType = AuthorizationTypeBearer
a.value = token
return a
}
// Basic sets the authorization to basic
func (a *authorizationImpl) Basic(username, password string) Authorization {
a.authorizationType = AuthorizationTypeBasic
a.value = username + ":" + password
return a
}
// String returns the string representation of the authorization
func (a *authorizationImpl) String() string {
return string(a.authorizationType) + " " + a.value
}
// Type returns the type of the authorization
func (a *authorizationImpl) Type() AuthorizationType {
return a.authorizationType
}
// Value returns the value of the authorization
func (a *authorizationImpl) Value() string {
return a.value
}
// IsBasic returns true if the authorization is basic
func (a *authorizationImpl) IsBasic() bool {
return a.authorizationType == AuthorizationTypeBasic
}
// IsBearer returns true if the authorization is bearer
func (a *authorizationImpl) IsBearer() bool {
return a.authorizationType == AuthorizationTypeBearer
}
// IsEmpty returns true if the authorization is empty
func (a *authorizationImpl) IsEmpty() bool {
return a.authorizationType == "" && a.value == ""
}
// IsSet returns true if the authorization is set
func (a *authorizationImpl) IsSet() bool {
return !a.IsEmpty()
}
// NewAuthorization creates a new authorization
func NewAuthorization() Authorization {
return &authorizationImpl{}
}