forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
132 lines (121 loc) · 3.93 KB
/
auth.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
package iks
import (
"encoding/json"
"io/ioutil"
gohttp "net/http"
"os"
"os/user"
ibmcloud "github.com/IBM-Cloud/bluemix-go"
"github.com/IBM-Cloud/bluemix-go/http"
"github.com/IBM-Cloud/bluemix-go/rest"
)
type ConfigJSON struct {
APIEndpoint string `json:"APIEndpoint"`
ConsoleEndpoint string `json:"ConsoleEndpoint"`
Region string `json:"Region"`
RegionID string `json:"RegionID"`
RegionType string `json:"RegionType"`
IAMEndpoint string `json:"IAMEndpoint"`
IAMToken string `json:"IAMToken"`
IAMRefreshToken string `json:"IAMRefreshToken"`
Account struct {
GUID string `json:"GUID"`
Name string `json:"Name"`
Owner string `json:"Owner"`
} `json:"Account"`
ResourceGroup struct {
GUID string `json:"GUID"`
Name string `json:"Name"`
State string `json:"State"`
Default bool `json:"Default"`
QuotaID string `json:"QuotaID"`
} `json:"ResourceGroup"`
CFEETargeted bool `json:"CFEETargeted"`
CFEEEnvID string `json:"CFEEEnvID"`
PluginRepos []struct {
Name string `json:"Name"`
URL string `json:"URL"`
} `json:"PluginRepos"`
SSLDisabled bool `json:"SSLDisabled"`
Locale string `json:"Locale"`
Trace string `json:"Trace"`
ColorEnabled string `json:"ColorEnabled"`
HTTPTimeout int `json:"HTTPTimeout"`
CLIInfoEndpoint string `json:"CLIInfoEndpoint"`
CheckCLIVersionDisabled bool `json:"CheckCLIVersionDisabled"`
UsageStatsDisabled bool `json:"UsageStatsDisabled"`
SDKVersion string `json:"SDKVersion"`
UpdateCheckInterval int `json:"UpdateCheckInterval"`
UpdateRetryCheckInterval int `json:"UpdateRetryCheckInterval"`
UpdateNotificationInterval int `json:"UpdateNotificationInterval"`
}
func ConfigFromJSON(config *ibmcloud.Config) (accountID string, err error) {
configjson := new(ConfigJSON)
usr, err := user.Current()
if err == nil {
jsonFile, err := os.Open(usr.HomeDir + "/.bluemix/config.json")
if err == nil {
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, configjson)
config.Region = configjson.Region
config.IAMAccessToken = configjson.IAMToken
config.IAMRefreshToken = configjson.IAMRefreshToken
config.SSLDisable = configjson.SSLDisabled
config.Region = configjson.Region
config.BluemixAPIKey = "fake"
config.IBMID = "fake"
config.IBMIDPassword = "fake"
accountID = configjson.Account.GUID
}
}
return
}
func getIAMAuthRepository(config *ibmcloud.Config) (*IAMAuthRepository, error) {
if config.HTTPClient == nil {
config.HTTPClient = http.NewHTTPClient(config)
}
return NewIAMAuthRepository(config, &rest.Client{
DefaultHeader: gohttp.Header{
"User-Agent": []string{http.UserAgent()},
},
HTTPClient: config.HTTPClient,
})
}
func AuthenticateSSO(passcode string, config *ibmcloud.Config) error {
config.IBMIDPassword = passcode
config.IBMID = passcode
config.Endpoint = nil
iamauthrepo, err := getIAMAuthRepository(config)
if err != nil {
return nil
}
return iamauthrepo.AuthenticateSSO(passcode)
}
func AuthenticatePassword(username string, password string, config *ibmcloud.Config) error {
config.IBMIDPassword = password
config.IBMID = username
config.Endpoint = nil
iamauthrepo, err := getIAMAuthRepository(config)
if err != nil {
return nil
}
return iamauthrepo.AuthenticatePassword(username, password)
}
func AuthenticateAPIKey(apikey string, config *ibmcloud.Config) error {
config.BluemixAPIKey = apikey
config.Endpoint = nil
iamauthrepo, err := getIAMAuthRepository(config)
if err != nil {
return nil
}
return iamauthrepo.AuthenticateAPIKey(apikey)
}
func RefreshTokenToLinkAccounts(account *Account, config *ibmcloud.Config) error {
config.Endpoint = nil
iamauthrepo, err := getIAMAuthRepository(config)
if err != nil {
return nil
}
return iamauthrepo.RefreshTokenToLinkAccounts(account)
}