-
Notifications
You must be signed in to change notification settings - Fork 69
Add SetAuthentication function to authenticate 'passively' #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,7 +91,6 @@ type Connector struct { | |
| apiKey string | ||
| accessToken string | ||
| verbose bool | ||
| user *userDetails | ||
| trust *x509.CertPool | ||
| zone cloudZone | ||
| client *http.Client | ||
|
|
@@ -135,35 +134,45 @@ func (c *Connector) Ping() (err error) { | |
| return nil | ||
| } | ||
|
|
||
| // Authenticate authenticates the user with Venafi Cloud using the provided API Key | ||
| // Authenticate sets the authentication credentials for the Venafi Cloud API. | ||
| // It will send a request to the API to verify the credentials are correct. | ||
| func (c *Connector) Authenticate(auth *endpoint.Authentication) error { | ||
| if err := c.SetAuthentication(auth); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if _, err := c.getUserDetails(); err != nil { | ||
| return fmt.Errorf("%w: %s", verror.AuthError, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // SetAuthentication sets the authentication credentials for the Venafi Cloud API. | ||
| func (c *Connector) SetAuthentication(auth *endpoint.Authentication) (err error) { | ||
| defer func() { | ||
| if err != nil { | ||
| err = fmt.Errorf("%w: %s", verror.AuthError, err) | ||
| } | ||
| }() | ||
|
|
||
| if auth == nil { | ||
| return fmt.Errorf("failed to authenticate: missing credentials") | ||
| } | ||
|
|
||
| //1. Access token. Assign it to connector | ||
| if auth.AccessToken != "" { | ||
| // 1. Access token. Assign it to connector | ||
| c.accessToken = auth.AccessToken | ||
| } else if auth.TokenURL != "" && auth.ExternalJWT != "" { | ||
| //2. JWT and token URL. use it to request new access token | ||
| // 2. JWT and token URL. use it to request new access token | ||
| tokenResponse, err := c.GetAccessToken(auth) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| c.accessToken = tokenResponse.AccessToken | ||
| } else if auth.APIKey != "" { | ||
| // 3. API key. Get user to test authentication | ||
| // 3. API key. Assign it to connector | ||
| c.apiKey = auth.APIKey | ||
| url := c.getURL(urlResourceUserAccounts) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check has been replaced by the |
||
| statusCode, status, body, err := c.request("GET", url, nil, true) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| ud, err := parseUserDetailsResult(http.StatusOK, statusCode, status, body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| c.user = ud | ||
| } | ||
|
|
||
| // Initialize clients | ||
|
|
@@ -948,7 +957,7 @@ func (c *Connector) isAuthenticated() bool { | |
| return true | ||
| } | ||
|
|
||
| if c.user != nil && c.user.Company != nil { | ||
| if c.apiKey != "" { | ||
| return true | ||
| } | ||
|
|
||
|
|
@@ -1456,12 +1465,10 @@ func (c *Connector) CreateUserAccount(userAccount *userAccount) (int, *userDetai | |
| if err != nil { | ||
| return statusCode, nil, err | ||
| } | ||
| //c.user = ud | ||
| return statusCode, ud, nil | ||
| } | ||
|
|
||
| func (c *Connector) getUserDetails() (*userDetails, error) { | ||
|
|
||
| url := c.getURL(urlResourceUserAccounts) | ||
| statusCode, status, body, err := c.request("GET", url, nil) | ||
| if err != nil { | ||
|
|
@@ -1471,7 +1478,6 @@ func (c *Connector) getUserDetails() (*userDetails, error) { | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| c.user = ud | ||
| return ud, nil | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,7 +45,6 @@ type Connector struct { | |
| apiKey string | ||
| accessToken string | ||
| verbose bool | ||
| Identity identity | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| trust *x509.CertPool | ||
| zone string | ||
| client *http.Client | ||
|
|
@@ -118,8 +117,22 @@ func (c *Connector) Ping() (err error) { | |
| return | ||
| } | ||
|
|
||
| // Authenticate authenticates the user to the TPP | ||
| func (c *Connector) Authenticate(auth *endpoint.Authentication) (err error) { | ||
| // Authenticate sets the Authentication details for the TPP Server and | ||
| // verifies that it can retrieve Self Identity. | ||
| func (c *Connector) Authenticate(auth *endpoint.Authentication) error { | ||
| if err := c.SetAuthentication(auth); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if _, err := c.retrieveSelfIdentity(); err != nil { | ||
| return fmt.Errorf("%w: %s", verror.AuthError, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // SetAuthentication sets the Authentication details for the TPP Server. | ||
| func (c *Connector) SetAuthentication(auth *endpoint.Authentication) (err error) { | ||
| defer func() { | ||
| if err != nil { | ||
| err = fmt.Errorf("%w: %s", verror.AuthError, err) | ||
|
|
@@ -143,13 +156,6 @@ func (c *Connector) Authenticate(auth *endpoint.Authentication) (err error) { | |
|
|
||
| resp := result.(authorizeResponse) | ||
| c.apiKey = resp.APIKey | ||
|
|
||
| if c.client != nil { | ||
| c.Identity, err = c.retrieveSelfIdentity() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These calls have been moved to the |
||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
|
|
||
| } else if auth.RefreshToken != "" { | ||
|
|
@@ -161,24 +167,12 @@ func (c *Connector) Authenticate(auth *endpoint.Authentication) (err error) { | |
|
|
||
| resp := result.(OauthRefreshAccessTokenResponse) | ||
| c.accessToken = resp.Access_token | ||
| auth.AccessToken = resp.Access_token | ||
| auth.RefreshToken = resp.Refresh_token | ||
| if c.client != nil { | ||
| c.Identity, err = c.retrieveSelfIdentity() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
|
|
||
| } else if auth.AccessToken != "" { | ||
| c.accessToken = auth.AccessToken | ||
|
|
||
| if c.client != nil { | ||
| c.Identity, err = c.retrieveSelfIdentity() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| return fmt.Errorf("failed to authenticate: can't determine valid credentials set") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
userwas not meaningfully used anywhere.