forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_authenticator.go
65 lines (54 loc) · 1.63 KB
/
fake_authenticator.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
package fakes
import (
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
)
type FakeAuthenticationRepository struct {
Config core_config.ReadWriter
AuthenticateArgs struct {
Credentials []map[string]string
}
GetLoginPromptsWasCalled bool
GetLoginPromptsReturns struct {
Error error
Prompts map[string]core_config.AuthPrompt
}
AuthError bool
AccessToken string
RefreshToken string
RefreshTokenCalled bool
RefreshTokenError error
}
func (auth *FakeAuthenticationRepository) Authenticate(credentials map[string]string) (apiErr error) {
auth.AuthenticateArgs.Credentials = append(auth.AuthenticateArgs.Credentials, copyMap(credentials))
if auth.AuthError {
apiErr = errors.New("Error authenticating.")
return
}
if auth.AccessToken == "" {
auth.AccessToken = "BEARER some_access_token"
}
auth.Config.SetAccessToken(auth.AccessToken)
auth.Config.SetRefreshToken(auth.RefreshToken)
return
}
func (auth *FakeAuthenticationRepository) RefreshAuthToken() (string, error) {
auth.RefreshTokenCalled = true
if auth.RefreshTokenError == nil {
return auth.RefreshToken, nil
}
return "", auth.RefreshTokenError
}
func (auth *FakeAuthenticationRepository) GetLoginPromptsAndSaveUAAServerURL() (prompts map[string]core_config.AuthPrompt, apiErr error) {
auth.GetLoginPromptsWasCalled = true
prompts = auth.GetLoginPromptsReturns.Prompts
apiErr = auth.GetLoginPromptsReturns.Error
return
}
func copyMap(input map[string]string) map[string]string {
output := map[string]string{}
for key, val := range input {
output[key] = val
}
return output
}