Skip to content

Commit

Permalink
Merge pull request #24 from lemoncloud-io/feature/louis-add-change-site
Browse files Browse the repository at this point in the history
feat: add changeUserSite method to change user site
  • Loading branch information
louis-lemon authored Jun 11, 2024
2 parents 3db4972 + 236bcbe commit 0fc49b9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/core/aws-web.core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
AWSWebCoreState,
Body,
ChangeSiteBody,
HttpResponse,
LemonCredentials,
LemonKMS,
Expand Down Expand Up @@ -260,6 +261,47 @@ export class AWSWebCore implements WebCoreService {
return await this.buildCredentialsByToken(refreshToken);
}

/**
* Changes the user site and returns new AWS credentials.
*
* @param {string} authId - user authId. It same as identityId in UserProfileView
* @param {ChangeSiteBody} changeSiteBody - The body containing site change details.
* @param {string} [url] - Optional URL for the OAuth endpoint.
* @returns {Promise<AWS.Credentials>} - A promise that resolves to AWS credentials.
* @throws Will throw an error if `authId`, `changeSiteBody`, `changeSiteBody.siteId`, or `changeSiteBody.userId` are not provided.
*
* @example
* const changeSiteBody = { siteId: 'newSiteId', userId: 'userId123' };
* const credentials = await changeUserSite(changeSiteBody);
*/
async changeUserSite(authId: string, changeSiteBody: ChangeSiteBody, url?: string): Promise<AWS.Credentials> {
if (!authId) {
throw new Error('@authId required');
}
if (!changeSiteBody || !changeSiteBody.siteId || !changeSiteBody.userId) {
throw new Error('@changeSiteBody required');
}

const cached = await this.tokenStorage.getCachedOAuthToken();
const target = `${changeSiteBody.userId}@${changeSiteBody.siteId}`;
const tokenSignature = await this.getTokenSignature();
const { current, signature, originToken } = tokenSignature;

const response: HttpResponse<LemonOAuthToken> = await this.signedRequest(
'POST',
url ? url : `${this.config.oAuthEndpoint}/oauth/${authId}/refresh`,
{},
{ current, signature, target }
);
const refreshToken = {
...response.data,
identityToken: response.data?.identityToken || originToken.identityToken,
identityPoolId: cached.identityPoolId,
};
this.logger.info('success to change user site');
return await this.buildCredentialsByToken(refreshToken);
}

/**
* Logs out the user.
* @returns {Promise<boolean>} - A promise that resolves to false.
Expand Down
15 changes: 15 additions & 0 deletions src/types/lemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,18 @@ export interface TokenSignature {
*/
originToken: LemonOAuthToken;
}

/**
* Represents the body of the request to change the user site.
*/
export interface ChangeSiteBody {
/**
* The ID of the user.
*/
userId: string;

/**
* The ID of the site to switch to.
*/
siteId: string;
}

0 comments on commit 0fc49b9

Please sign in to comment.