Skip to content

Commit 0aa3e83

Browse files
authored
Migrating ImportUsers() API to internal.HTTPClient (#241)
* Migrating DeleteUser() API to the internal.HTTPClient * Migrated ImportUsers() to internal.HTTPClient * Minor clean up
1 parent ac07281 commit 0aa3e83

File tree

5 files changed

+234
-258
lines changed

5 files changed

+234
-258
lines changed

auth/hash/hash.go

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
type Bcrypt struct{}
3232

3333
// Config returns the validated hash configuration.
34-
func (b Bcrypt) Config() (*internal.HashConfig, error) {
35-
return &internal.HashConfig{HashAlgorithm: "BCRYPT"}, nil
34+
func (b Bcrypt) Config() (internal.HashConfig, error) {
35+
return internal.HashConfig{"hashAlgorithm": "BCRYPT"}, nil
3636
}
3737

3838
// StandardScrypt represents the standard scrypt hash algorithm.
@@ -47,14 +47,13 @@ type StandardScrypt struct {
4747
}
4848

4949
// Config returns the validated hash configuration.
50-
func (s StandardScrypt) Config() (*internal.HashConfig, error) {
51-
return &internal.HashConfig{
52-
HashAlgorithm: "STANDARD_SCRYPT",
53-
DerivedKeyLength: int64(s.DerivedKeyLength),
54-
BlockSize: int64(s.BlockSize),
55-
Parallelization: int64(s.Parallelization),
56-
MemoryCost: int64(s.MemoryCost),
57-
ForceSendFields: []string{"BlockSize", "Parallelization", "MemoryCost", "DkLen"},
50+
func (s StandardScrypt) Config() (internal.HashConfig, error) {
51+
return internal.HashConfig{
52+
"hashAlgorithm": "STANDARD_SCRYPT",
53+
"dkLen": s.DerivedKeyLength,
54+
"blockSize": s.BlockSize,
55+
"parallelization": s.Parallelization,
56+
"memoryCost": s.MemoryCost,
5857
}, nil
5958
}
6059

@@ -72,7 +71,7 @@ type Scrypt struct {
7271
}
7372

7473
// Config returns the validated hash configuration.
75-
func (s Scrypt) Config() (*internal.HashConfig, error) {
74+
func (s Scrypt) Config() (internal.HashConfig, error) {
7675
if len(s.Key) == 0 {
7776
return nil, errors.New("signer key not specified")
7877
}
@@ -82,12 +81,12 @@ func (s Scrypt) Config() (*internal.HashConfig, error) {
8281
if s.MemoryCost < 1 || s.MemoryCost > 14 {
8382
return nil, errors.New("memory cost must be between 1 and 14")
8483
}
85-
return &internal.HashConfig{
86-
HashAlgorithm: "SCRYPT",
87-
SignerKey: base64.RawURLEncoding.EncodeToString(s.Key),
88-
SaltSeparator: base64.RawURLEncoding.EncodeToString(s.SaltSeparator),
89-
Rounds: int64(s.Rounds),
90-
MemoryCost: int64(s.MemoryCost),
84+
return internal.HashConfig{
85+
"hashAlgorithm": "SCRYPT",
86+
"signerKey": base64.RawURLEncoding.EncodeToString(s.Key),
87+
"saltSeparator": base64.RawURLEncoding.EncodeToString(s.SaltSeparator),
88+
"rounds": s.Rounds,
89+
"memoryCost": s.MemoryCost,
9190
}, nil
9291
}
9392

@@ -100,7 +99,7 @@ type HMACMD5 struct {
10099
}
101100

102101
// Config returns the validated hash configuration.
103-
func (h HMACMD5) Config() (*internal.HashConfig, error) {
102+
func (h HMACMD5) Config() (internal.HashConfig, error) {
104103
return hmacConfig("HMAC_MD5", h.Key)
105104
}
106105

@@ -114,7 +113,7 @@ type HMACSHA1 struct {
114113
}
115114

116115
// Config returns the validated hash configuration.
117-
func (h HMACSHA1) Config() (*internal.HashConfig, error) {
116+
func (h HMACSHA1) Config() (internal.HashConfig, error) {
118117
return hmacConfig("HMAC_SHA1", h.Key)
119118
}
120119

@@ -128,7 +127,7 @@ type HMACSHA256 struct {
128127
}
129128

130129
// Config returns the validated hash configuration.
131-
func (h HMACSHA256) Config() (*internal.HashConfig, error) {
130+
func (h HMACSHA256) Config() (internal.HashConfig, error) {
132131
return hmacConfig("HMAC_SHA256", h.Key)
133132
}
134133

@@ -142,7 +141,7 @@ type HMACSHA512 struct {
142141
}
143142

144143
// Config returns the validated hash configuration.
145-
func (h HMACSHA512) Config() (*internal.HashConfig, error) {
144+
func (h HMACSHA512) Config() (internal.HashConfig, error) {
146145
return hmacConfig("HMAC_SHA512", h.Key)
147146
}
148147

@@ -156,7 +155,7 @@ type MD5 struct {
156155
}
157156

158157
// Config returns the validated hash configuration.
159-
func (h MD5) Config() (*internal.HashConfig, error) {
158+
func (h MD5) Config() (internal.HashConfig, error) {
160159
return basicConfig("MD5", h.Rounds)
161160
}
162161

@@ -170,7 +169,7 @@ type PBKDF2SHA256 struct {
170169
}
171170

172171
// Config returns the validated hash configuration.
173-
func (h PBKDF2SHA256) Config() (*internal.HashConfig, error) {
172+
func (h PBKDF2SHA256) Config() (internal.HashConfig, error) {
174173
return basicConfig("PBKDF2_SHA256", h.Rounds)
175174
}
176175

@@ -184,7 +183,7 @@ type PBKDFSHA1 struct {
184183
}
185184

186185
// Config returns the validated hash configuration.
187-
func (h PBKDFSHA1) Config() (*internal.HashConfig, error) {
186+
func (h PBKDFSHA1) Config() (internal.HashConfig, error) {
188187
return basicConfig("PBKDF_SHA1", h.Rounds)
189188
}
190189

@@ -198,7 +197,7 @@ type SHA1 struct {
198197
}
199198

200199
// Config returns the validated hash configuration.
201-
func (h SHA1) Config() (*internal.HashConfig, error) {
200+
func (h SHA1) Config() (internal.HashConfig, error) {
202201
return basicConfig("SHA1", h.Rounds)
203202
}
204203

@@ -212,7 +211,7 @@ type SHA256 struct {
212211
}
213212

214213
// Config returns the validated hash configuration.
215-
func (h SHA256) Config() (*internal.HashConfig, error) {
214+
func (h SHA256) Config() (internal.HashConfig, error) {
216215
return basicConfig("SHA256", h.Rounds)
217216
}
218217

@@ -226,27 +225,26 @@ type SHA512 struct {
226225
}
227226

228227
// Config returns the validated hash configuration.
229-
func (h SHA512) Config() (*internal.HashConfig, error) {
228+
func (h SHA512) Config() (internal.HashConfig, error) {
230229
return basicConfig("SHA512", h.Rounds)
231230
}
232231

233-
func hmacConfig(name string, key []byte) (*internal.HashConfig, error) {
232+
func hmacConfig(name string, key []byte) (internal.HashConfig, error) {
234233
if len(key) == 0 {
235234
return nil, errors.New("signer key not specified")
236235
}
237-
return &internal.HashConfig{
238-
HashAlgorithm: name,
239-
SignerKey: base64.RawURLEncoding.EncodeToString(key),
236+
return internal.HashConfig{
237+
"hashAlgorithm": name,
238+
"signerKey": base64.RawURLEncoding.EncodeToString(key),
240239
}, nil
241240
}
242241

243-
func basicConfig(name string, rounds int) (*internal.HashConfig, error) {
242+
func basicConfig(name string, rounds int) (internal.HashConfig, error) {
244243
if rounds < 0 || rounds > 120000 {
245244
return nil, errors.New("rounds must be between 0 and 120000")
246245
}
247-
return &internal.HashConfig{
248-
HashAlgorithm: name,
249-
Rounds: int64(rounds),
250-
ForceSendFields: []string{"Rounds"},
246+
return internal.HashConfig{
247+
"hashAlgorithm": name,
248+
"rounds": rounds,
251249
}, nil
252250
}

auth/hash/hash_test.go

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var validHashes = []struct {
3434
}{
3535
{
3636
alg: Bcrypt{},
37-
want: internal.HashConfig{HashAlgorithm: "BCRYPT"},
37+
want: internal.HashConfig{"hashAlgorithm": "BCRYPT"},
3838
},
3939
{
4040
alg: StandardScrypt{
@@ -44,12 +44,11 @@ var validHashes = []struct {
4444
MemoryCost: 4,
4545
},
4646
want: internal.HashConfig{
47-
HashAlgorithm: "STANDARD_SCRYPT",
48-
BlockSize: 1,
49-
DerivedKeyLength: 2,
50-
Parallelization: 3,
51-
MemoryCost: 4,
52-
ForceSendFields: []string{"BlockSize", "Parallelization", "MemoryCost", "DkLen"},
47+
"hashAlgorithm": "STANDARD_SCRYPT",
48+
"blockSize": 1,
49+
"dkLen": 2,
50+
"parallelization": 3,
51+
"memoryCost": 4,
5352
},
5453
},
5554
{
@@ -60,87 +59,81 @@ var validHashes = []struct {
6059
MemoryCost: 14,
6160
},
6261
want: internal.HashConfig{
63-
HashAlgorithm: "SCRYPT",
64-
SignerKey: base64.RawURLEncoding.EncodeToString(signerKey),
65-
SaltSeparator: base64.RawURLEncoding.EncodeToString(saltSeparator),
66-
Rounds: 8,
67-
MemoryCost: 14,
62+
"hashAlgorithm": "SCRYPT",
63+
"signerKey": base64.RawURLEncoding.EncodeToString(signerKey),
64+
"saltSeparator": base64.RawURLEncoding.EncodeToString(saltSeparator),
65+
"rounds": 8,
66+
"memoryCost": 14,
6867
},
6968
},
7069
{
7170
alg: HMACMD5{signerKey},
7271
want: internal.HashConfig{
73-
HashAlgorithm: "HMAC_MD5",
74-
SignerKey: base64.RawURLEncoding.EncodeToString(signerKey),
72+
"hashAlgorithm": "HMAC_MD5",
73+
"signerKey": base64.RawURLEncoding.EncodeToString(signerKey),
7574
},
7675
},
7776
{
7877
alg: HMACSHA1{signerKey},
7978
want: internal.HashConfig{
80-
HashAlgorithm: "HMAC_SHA1",
81-
SignerKey: base64.RawURLEncoding.EncodeToString(signerKey),
79+
"hashAlgorithm": "HMAC_SHA1",
80+
"signerKey": base64.RawURLEncoding.EncodeToString(signerKey),
8281
},
8382
},
8483
{
8584
alg: HMACSHA256{signerKey},
8685
want: internal.HashConfig{
87-
HashAlgorithm: "HMAC_SHA256",
88-
SignerKey: base64.RawURLEncoding.EncodeToString(signerKey),
86+
"hashAlgorithm": "HMAC_SHA256",
87+
"signerKey": base64.RawURLEncoding.EncodeToString(signerKey),
8988
},
9089
},
9190
{
9291
alg: HMACSHA512{signerKey},
9392
want: internal.HashConfig{
94-
HashAlgorithm: "HMAC_SHA512",
95-
SignerKey: base64.RawURLEncoding.EncodeToString(signerKey),
93+
"hashAlgorithm": "HMAC_SHA512",
94+
"signerKey": base64.RawURLEncoding.EncodeToString(signerKey),
9695
},
9796
},
9897
{
9998
alg: MD5{42},
10099
want: internal.HashConfig{
101-
HashAlgorithm: "MD5",
102-
Rounds: 42,
103-
ForceSendFields: []string{"Rounds"},
100+
"hashAlgorithm": "MD5",
101+
"rounds": 42,
104102
},
105103
},
106104
{
107105
alg: SHA1{42},
108106
want: internal.HashConfig{
109-
HashAlgorithm: "SHA1",
110-
Rounds: 42,
111-
ForceSendFields: []string{"Rounds"},
107+
"hashAlgorithm": "SHA1",
108+
"rounds": 42,
112109
},
113110
},
114111
{
115112
alg: SHA256{42},
116113
want: internal.HashConfig{
117-
HashAlgorithm: "SHA256",
118-
Rounds: 42,
119-
ForceSendFields: []string{"Rounds"},
114+
"hashAlgorithm": "SHA256",
115+
"rounds": 42,
120116
},
121117
},
122118
{
123119
alg: SHA512{42},
124120
want: internal.HashConfig{
125-
HashAlgorithm: "SHA512",
126-
Rounds: 42,
127-
ForceSendFields: []string{"Rounds"},
121+
"hashAlgorithm": "SHA512",
122+
"rounds": 42,
128123
},
129124
},
130125
{
131126
alg: PBKDFSHA1{42},
132127
want: internal.HashConfig{
133-
HashAlgorithm: "PBKDF_SHA1",
134-
Rounds: 42,
135-
ForceSendFields: []string{"Rounds"},
128+
"hashAlgorithm": "PBKDF_SHA1",
129+
"rounds": 42,
136130
},
137131
},
138132
{
139133
alg: PBKDF2SHA256{42},
140134
want: internal.HashConfig{
141-
HashAlgorithm: "PBKDF2_SHA256",
142-
Rounds: 42,
143-
ForceSendFields: []string{"Rounds"},
135+
"hashAlgorithm": "PBKDF2_SHA256",
136+
"rounds": 42,
144137
},
145138
},
146139
}
@@ -262,7 +255,7 @@ func TestValidHash(t *testing.T) {
262255
got, err := tc.alg.Config()
263256
if err != nil {
264257
t.Errorf("[%d] Config() = %v", idx, err)
265-
} else if !reflect.DeepEqual(*got, tc.want) {
258+
} else if !reflect.DeepEqual(got, tc.want) {
266259
t.Errorf("[%d] Config() = %#v; want = %#v", idx, got, tc.want)
267260
}
268261
}

0 commit comments

Comments
 (0)