mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Move deactivation of token usage for authentication into Authenticate()
This commit is contained in:
parent
cdc54111a1
commit
4f3892368e
@ -14,10 +14,7 @@ var loginCmd = &cobra.Command{
|
||||
Short: "Log in to a Portainer instance",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Get auth token
|
||||
config := common.GetDefaultClientConfig()
|
||||
config.DoNotUseToken = true
|
||||
|
||||
client, err := common.NewClient(common.GetDefaultHttpClient(), config)
|
||||
client, err := common.GetDefaultClient()
|
||||
common.CheckError(err)
|
||||
|
||||
authToken, err := client.Authenticate()
|
||||
|
@ -77,9 +77,7 @@ func (n *PortainerClient) do(uri, method string, request io.Reader, requestType
|
||||
|
||||
if !n.doNotUseToken {
|
||||
if n.token == "" {
|
||||
clientClone := n.Clone()
|
||||
clientClone.doNotUseToken = true
|
||||
n.token, err = clientClone.Authenticate()
|
||||
n.token, err = n.Authenticate()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -144,11 +142,16 @@ func (n *PortainerClient) Authenticate() (token string, err error) {
|
||||
|
||||
respBody := AuthenticateUserResponse{}
|
||||
|
||||
previousDoNotUseTokenValue := n.doNotUseToken
|
||||
n.doNotUseToken = true
|
||||
|
||||
err = n.doJSON("auth", http.MethodPost, &reqBody, &respBody)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
n.doNotUseToken = previousDoNotUseTokenValue
|
||||
|
||||
token = respBody.Jwt
|
||||
|
||||
return
|
||||
@ -258,20 +261,6 @@ func (n *PortainerClient) GetStatus() (status Status, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get a clone of the client
|
||||
func (n *PortainerClient) Clone() (c *PortainerClient) {
|
||||
c = &PortainerClient{
|
||||
httpClient: n.httpClient,
|
||||
url: n.url,
|
||||
user: n.user,
|
||||
password: n.password,
|
||||
token: n.token,
|
||||
doNotUseToken: n.doNotUseToken,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Create a new client
|
||||
func NewClient(httpClient *http.Client, config ClientConfig) (c *PortainerClient, err error) {
|
||||
apiUrl, err := url.Parse(strings.TrimRight(config.Url, "/") + "/api/")
|
||||
|
Loading…
Reference in New Issue
Block a user