Skip to content

Commit

Permalink
feat: add access token support (#673)
Browse files Browse the repository at this point in the history
Co-authored-by: kakha urigashvili <kakuri@amazon.com>
  • Loading branch information
kakhaUrigashvili and kakha urigashvili committed Jan 8, 2021
1 parent 9522e94 commit 9151628
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
23 changes: 21 additions & 2 deletions ask-smapi-sdk/lib/smapiClientBuilder/AbstractSmapiClientBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
* permissions and limitations under the License.
*/

import { services } from 'ask-smapi-model';
import { RefreshTokenConfig } from './authMethods/AuthMethods';
import { runtime, services } from 'ask-smapi-model';
import AuthenticationConfiguration = runtime.AuthenticationConfiguration;
import { AccessTokenConfig, RefreshTokenConfig } from './authMethods/AuthMethods';

/**
* Abstract Builder class which should be implemented.
Expand All @@ -22,6 +23,7 @@ import { RefreshTokenConfig } from './authMethods/AuthMethods';
export class SmapiClientBuilder {

protected customUserAgent: string;
protected accessTokenConfig: AccessTokenConfig;
protected refreshTokenConfig: RefreshTokenConfig;

public withCustomUserAgent(userAgent: string): SmapiClientBuilder {
Expand All @@ -30,6 +32,12 @@ export class SmapiClientBuilder {
return this;
}

public withAccessTokenConfig(accessTokenConfig: AccessTokenConfig): SmapiClientBuilder {
this.accessTokenConfig = accessTokenConfig;

return this;
}

public withRefreshTokenConfig(refreshTokenConfig: RefreshTokenConfig): SmapiClientBuilder {
this.refreshTokenConfig = refreshTokenConfig;

Expand All @@ -39,4 +47,15 @@ export class SmapiClientBuilder {
public client(): services.skillManagement.SkillManagementServiceClient {
throw new Error('client function is not implemented');
}

protected getAuthenticationConfiguration(): AuthenticationConfiguration {
const authenticationConfiguration: AuthenticationConfiguration = {
clientId: this.refreshTokenConfig ? this.refreshTokenConfig.clientId : this.accessTokenConfig.clientId,
clientSecret: this.refreshTokenConfig ? this.refreshTokenConfig.clientSecret : this.accessTokenConfig.clientSecret,
accessToken: this.accessTokenConfig ? this.accessTokenConfig.accessToken : undefined,
refreshToken: this.refreshTokenConfig ? this.refreshTokenConfig.refreshToken : undefined
};

return authenticationConfiguration;
}
}
23 changes: 8 additions & 15 deletions ask-smapi-sdk/lib/smapiClientBuilder/SmapiClientBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,19 @@ export class StandardSmapiClientBuilder extends SmapiClientBuilder {
*/
public client(): services.skillManagement.SkillManagementServiceClient {

if (this.refreshTokenConfig) {
if (this.refreshTokenConfig || this.accessTokenConfig) {
const apiConfiguration: ApiConfiguration = {
apiClient: new DefaultApiClient(),
apiEndpoint: DEFAULT_API_ENDPOINT,
authorizationValue: null,
};
const authenticationConfiguration: AuthenticationConfiguration = {
clientId: this.refreshTokenConfig.clientId,
clientSecret: this.refreshTokenConfig.clientSecret,
refreshToken: this.refreshTokenConfig.refreshToken,
};

const authenticationConfiguration: AuthenticationConfiguration = this.getAuthenticationConfiguration();

return new services.skillManagement.SkillManagementServiceClient(apiConfiguration, authenticationConfiguration, this.customUserAgent);
}

throw new Error('Please provide refreshToken Config to build smapi client');
throw new Error('Please provide accessToken or refreshToken Config to build smapi client');
}
}

Expand Down Expand Up @@ -87,22 +84,18 @@ export class CustomSmapiClientBuilder extends StandardSmapiClientBuilder {
this.apiClient = new DefaultApiClient();
}

if (this.refreshTokenConfig) {
if (this.refreshTokenConfig || this.accessTokenConfig) {
const apiConfiguration: ApiConfiguration = {
apiClient: this.apiClient,
apiEndpoint: this.apiEndpoint,
authorizationValue: null,
};
const authenticationConfiguration: AuthenticationConfiguration = {
clientId: this.refreshTokenConfig.clientId,
clientSecret: this.refreshTokenConfig.clientSecret,
refreshToken: this.refreshTokenConfig.refreshToken,
authEndpoint: this.authEndpoint,
};
const authenticationConfiguration: AuthenticationConfiguration = this.getAuthenticationConfiguration();
authenticationConfiguration.authEndpoint = this.authEndpoint;

return new services.skillManagement.SkillManagementServiceClient(apiConfiguration, authenticationConfiguration, this.customUserAgent);
}

throw new Error('Please provide refreshToken Config to build smapi client');
throw new Error('Please provide accessToken or refreshToken Config to build smapi client');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ export interface RefreshTokenConfig {
clientSecret: string;
refreshToken: string;
}

export interface AccessTokenConfig {
clientId: string;
clientSecret: string;
accessToken: string;
}

0 comments on commit 9151628

Please sign in to comment.