-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.go
183 lines (153 loc) · 4.26 KB
/
types.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
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
package common
import (
"context"
"crypto/rsa"
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
"time"
"gorm.io/gorm"
)
var supportedtruevalues = []string{"true", "t", "yes", "y", "on"}
// Ptr returns pointer to any type.
func Ptr[T any](v T) *T {
return &v
}
// PtrValue returns value of any type.
func PtrValue[T any](p *T) T {
if p != nil {
return *p
}
var v T
return v
}
// String returns pointer to string.
func String(s string) *string {
return Ptr(s)
}
// StringValue returns string value from pointervalue.
func StringValue(s *string) string {
return PtrValue(s)
}
// Int returns pointer to int.
func Int(value int) *int {
return Ptr(value)
}
// Int64 returns pointer to int64.
func Int64(value int64) *int64 {
return Ptr(value)
}
// MapToString modifies map to string array.
func MapToString(input map[string]string) []string {
result := []string{}
for key, val := range input {
result = append(result, fmt.Sprintf("%s=%s", key, val))
}
return result
}
// Int64Value returns value from pointer.
func Int64Value(v *int64) int64 {
return PtrValue(v)
}
// Int32 returns pointer to int32.
func Int32(value int32) *int32 {
return Ptr(value)
}
// Int32Value returns value from pointer.
func Int32Value(v *int32) int32 {
return PtrValue(v)
}
// UintValue returns value from pointer.
func UintValue(v *uint) uint {
return PtrValue(v)
}
// Float64Value returns value from pointer.
func Float64Value(v *float64) float64 {
return PtrValue(v)
}
// Float64 returns pointer to float64.
func Float64(value float64) *float64 {
return Ptr(value)
}
// Bool returns a pointer to a bool.
func Bool(v bool) *bool {
return Ptr(v)
}
// BoolValue returns the value of bool pointer or false.
func BoolValue(v *bool) bool {
return PtrValue(v)
}
// StringToBool returns boolean value from string.
func StringToBool(value string) bool {
return ContainsString(supportedtruevalues, strings.ToLower(value))
}
// StringEmpty returns boolean value if string is empty.
func StringEmpty(value string) bool {
return value == ""
}
// Model is tuned gorm.model.
type Model struct {
ID uint `json:"id" gorm:"primarykey"`
CreatedAt time.Time `gorm:"index"`
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
// JWTKey is struct for storing auth private keys.
type JWTKey struct {
Model
KID string `yaml:"kid" json:"kid"`
PrivateKey *rsa.PrivateKey `yaml:"-" json:"-" gorm:"-"`
PrivateKeyAsBytes []byte `yaml:"-" json:"-"`
PublicKey *rsa.PublicKey `yaml:"-" json:"-" gorm:"-"`
PublicKeyAsBytes []byte `yaml:"-" json:"-"`
}
// Datastore will contain interface to store auth keys.
type Datastore interface {
AddJWTKey(context.Context, JWTKey) (*JWTKey, error)
ListJWTKeys(context.Context) ([]JWTKey, error)
RotateJWTKeys(context.Context, uint) error
}
// Internal contains struct for internal non standard variables.
type Internal struct {
Cluster *string `json:"cluster,omitempty"`
ChangeLimit *int `json:"limit,omitempty"`
MFA *bool `json:"mfa"`
EmployeeID string `json:"employeeid,omitempty"`
}
// User contains struct for single user.
type User struct {
Groups []string `json:"groups,omitempty"`
Eid string `json:"custom:employeeid,omitempty"`
ImportGroups []string `json:"cognito:groups,omitempty"`
Email *string `json:"email,omitempty"`
EmailVerified *bool `json:"email_verified,omitempty"`
Name *string `json:"name,omitempty"`
Internal *Internal `json:"internal,omitempty"`
}
// MakeSub returns sub value for user.
func (u *User) MakeSub() string {
if u == nil {
return ""
}
sub := StringValue(u.Email)
if u.Internal != nil && u.Internal.EmployeeID != "" {
sub = u.Internal.EmployeeID
}
sub = strings.ToLower(sub)
b := sha256.Sum256([]byte(sub))
return hex.EncodeToString(b[:])
}
// ServiceAccountPrefix email domain for service accounts.
const ServiceAccountPrefix = "@oauth2"
// IsServiceAccount returns boolean is the account service account.
func (u User) IsServiceAccount() bool {
return strings.HasSuffix(StringValue(u.Email), ServiceAccountPrefix)
}
// TokenMFA returns state does user has MFA used in current JWT.
func (u User) TokenMFA() bool {
if u.Internal == nil {
return false
}
return BoolValue(u.Internal.MFA)
}