-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
design proposal for refresh token authenticator
- Loading branch information
Showing
7 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package openapi | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Api Key Header Auth | ||
type apiRefreshTokenAuthenticator struct { | ||
apiKey | ||
refreshTokenURL string | ||
} | ||
|
||
func newAPIRefreshTokenAuthenticator(name, refreshToken, refreshTokenURL string) apiRefreshTokenAuthenticator { | ||
return apiRefreshTokenAuthenticator{ | ||
apiKey: apiKey{ | ||
name: name, | ||
value: refreshToken, | ||
}, | ||
refreshTokenURL: refreshTokenURL, | ||
} | ||
} | ||
|
||
func (a apiRefreshTokenAuthenticator) getContext() interface{} { | ||
return a.apiKey | ||
} | ||
|
||
func (a apiRefreshTokenAuthenticator) getType() authType { | ||
return authTypeAPIKeyHeader | ||
} | ||
|
||
// prepareAPIKeyAuthentication adds to the map the auth header required for apikey header authentication. The url | ||
// remains the same | ||
func (a apiRefreshTokenAuthenticator) prepareAuth(authContext *authContext) error { | ||
apiKey := a.getContext().(apiKey) | ||
|
||
authorizationHeaderValue := apiKey.value | ||
fmt.Println(authorizationHeaderValue) | ||
// TODO: call refresh token API (POST) a.refreshTokenURL including the refresh token passed in as Authorization header with value apiKey.value | ||
// Get the access token from the response header Authorization | ||
// Strip out the bearer scheme from the header value and return the string | ||
|
||
authContext.headers[apiKey.name] = "access token" | ||
return nil | ||
} |
54 changes: 54 additions & 0 deletions
54
openapi/openapi_spec_security_definition_apikey_refresh_token.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package openapi | ||
|
||
import ( | ||
"fmt" | ||
"github.com/dikhan/terraform-provider-openapi/openapi/terraformutils" | ||
"github.com/go-openapi/spec" | ||
"strings" | ||
) | ||
|
||
type specAPIKeyHeaderCustomAuthSecurityDefinition struct { | ||
name string | ||
secDef *spec.SecurityScheme | ||
refreshTokenURL string | ||
} | ||
|
||
// newAPIKeyHeaderBearerSecurityDefinition constructs a SpecSecurityDefinition of Header type using the Bearer authentication | ||
// scheme. The secDefName value is the identifier of the security definition, and the apiKeyName is the actual value of the header/query that will be user in the HTTP request. | ||
func newAPIKeyHeaderRefreshTokenSecurityDefinition(secDefName string, secDef *spec.SecurityScheme, refreshTokenURL string) specAPIKeyHeaderCustomAuthSecurityDefinition { | ||
return specAPIKeyHeaderCustomAuthSecurityDefinition{secDefName, secDef, refreshTokenURL} | ||
} | ||
|
||
func (s specAPIKeyHeaderCustomAuthSecurityDefinition) getName() string { | ||
return s.name | ||
} | ||
|
||
func (s specAPIKeyHeaderCustomAuthSecurityDefinition) getType() securityDefinitionType { | ||
return securityDefinitionAPIKeyRefreshToken | ||
} | ||
|
||
func (s specAPIKeyHeaderCustomAuthSecurityDefinition) getTerraformConfigurationName() string { | ||
return terraformutils.ConvertToTerraformCompliantName(s.name) | ||
} | ||
|
||
func (s specAPIKeyHeaderCustomAuthSecurityDefinition) getAPIKey() specAPIKey { | ||
apiKey := newAPIKeyHeader(authorization) | ||
apiKey.Metadata = map[apiKeyMetadataKey]interface{}{ | ||
refreshTokenURLKey: s.refreshTokenURL, | ||
} | ||
return apiKey | ||
} | ||
|
||
func (s specAPIKeyHeaderCustomAuthSecurityDefinition) buildValue(refreshToken string) string { | ||
if !strings.Contains(refreshToken, bearerScheme) { | ||
refreshToken = fmt.Sprintf("Bearer %s", refreshToken) | ||
} | ||
return refreshToken | ||
} | ||
|
||
func (s specAPIKeyHeaderCustomAuthSecurityDefinition) validate() error { | ||
if s.name == "" { | ||
return fmt.Errorf("specAPIKeyHeaderBearerSecurityDefinition missing mandatory security definition name") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters