Skip to content
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

Rename getFederatedConnectionAccessToken to getAccessTokenForConnection #1928

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export class AccessTokenError extends SdkError {
}

/**
* Enum representing error codes related to federated connection access tokens.
* Enum representing error codes related to access tokens for connections.
*/
export enum FederatedConnectionAccessTokenErrorCode {
export enum AccessTokenForConnectionErrorCode {
/**
* The session is missing.
*/
Expand All @@ -120,24 +120,24 @@ export enum FederatedConnectionAccessTokenErrorCode {
}

/**
* Error class representing an access token error for federated connections.
* Error class representing an access token for connection error.
* Extends the `SdkError` class.
*/
export class FederatedConnectionsAccessTokenError extends SdkError {
export class AccessTokenForConnectionError extends SdkError {
/**
* The error code associated with the access token error.
*/
public code: string;

/**
* Constructs a new `FederatedConnectionsAccessTokenError` instance.
* Constructs a new `AccessTokenForConnectionError` instance.
*
* @param code - The error code.
* @param message - The error message.
*/
constructor(code: string, message: string) {
super(message);
this.name = "FederatedConnectionAccessTokenError";
this.name = "AccessTokenForConnectionError";
this.code = code;
}
}
38 changes: 19 additions & 19 deletions src/server/auth-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4154,8 +4154,8 @@ ca/T0LLtgmbMmxSv/MmzIg==
});
});

describe("getFederatedConnectionTokenSet", async () => {
it("should call for an access token when no federated connection token set in the session", async () => {
describe("getonnectionTokenSet", async () => {
it("should call for an access token when no connection token set in the session", async () => {
const secret = await generateSecret(32);
const transactionStore = new TransactionStore({
secret
Expand Down Expand Up @@ -4192,22 +4192,22 @@ ca/T0LLtgmbMmxSv/MmzIg==
expiresAt
};

const response = await authClient.getFederatedConnectionTokenSet(
const response = await authClient.getConnectionTokenSet(
tokenSet,
undefined,
{ connection: "google-oauth2", login_hint: "000100123" }
);
const [error, federatedConnectionTokenSet] = response;
const [error, connectionTokenSet] = response;
expect(error).toBe(null);
expect(fetchSpy).toHaveBeenCalled();
expect(federatedConnectionTokenSet).toEqual({
expect(connectionTokenSet).toEqual({
accessToken: DEFAULT.accessToken,
connection: "google-oauth2",
expiresAt: expect.any(Number)
});
});

it("should return access token from the session when federated connection token set in the session is not expired", async () => {
it("should return access token from the session when connection token set in the session is not expired", async () => {
const secret = await generateSecret(32);
const transactionStore = new TransactionStore({
secret
Expand Down Expand Up @@ -4237,22 +4237,22 @@ ca/T0LLtgmbMmxSv/MmzIg==
expiresAt,
};

const response = await authClient.getFederatedConnectionTokenSet(
const response = await authClient.getConnectionTokenSet(
tokenSet,
{ connection: 'google-oauth2', accessToken: 'fc_at', expiresAt: Math.floor(Date.now() / 1000) + 86400 },
{ connection: "google-oauth2", login_hint: "000100123" }
);
const [error, federatedConnectionTokenSet] = response;
const [error, connectionTokenSet] = response;
expect(error).toBe(null);
expect(federatedConnectionTokenSet).toEqual({
expect(connectionTokenSet).toEqual({
accessToken: 'fc_at',
connection: "google-oauth2",
expiresAt: expect.any(Number)
});
expect(fetchSpy).not.toHaveBeenCalled();
});

it("should call for an access token when federated connection token set in the session is expired", async () => {
it("should call for an access token when connection token set in the session is expired", async () => {
const secret = await generateSecret(32);
const transactionStore = new TransactionStore({
secret
Expand Down Expand Up @@ -4288,14 +4288,14 @@ ca/T0LLtgmbMmxSv/MmzIg==
expiresAt,
};

const response = await authClient.getFederatedConnectionTokenSet(
const response = await authClient.getConnectionTokenSet(
tokenSet,
{ connection: 'google-oauth2', accessToken: 'fc_at', expiresAt },
{ connection: "google-oauth2", login_hint: "000100123" }
);
const [error, federatedConnectionTokenSet] = response;
const [error, connectionTokenSet] = response;
expect(error).toBe(null);
expect(federatedConnectionTokenSet).toEqual({
expect(connectionTokenSet).toEqual({
accessToken: DEFAULT.accessToken,
connection: "google-oauth2",
expiresAt: expect.any(Number)
Expand Down Expand Up @@ -4334,12 +4334,12 @@ ca/T0LLtgmbMmxSv/MmzIg==
expiresAt
};

const [error, federatedConnectionTokenSet] =
await authClient.getFederatedConnectionTokenSet(tokenSet, undefined, {
const [error, connectionTokenSet] =
await authClient.getConnectionTokenSet(tokenSet, undefined, {
connection: "google-oauth2"
});
expect(error?.code).toEqual("discovery_error");
expect(federatedConnectionTokenSet).toBeNull();
expect(connectionTokenSet).toBeNull();
});

it("should return an error if the token set does not contain a refresh token", async () => {
Expand Down Expand Up @@ -4370,12 +4370,12 @@ ca/T0LLtgmbMmxSv/MmzIg==
expiresAt
};

const [error, federatedConnectionTokenSet] =
await authClient.getFederatedConnectionTokenSet(tokenSet, undefined, {
const [error, connectionTokenSet] =
await authClient.getConnectionTokenSet(tokenSet, undefined, {
connection: "google-oauth2"
});
expect(error?.code).toEqual("missing_refresh_token");
expect(federatedConnectionTokenSet).toBeNull();
expect(connectionTokenSet).toBeNull();
});
});
});
Expand Down
54 changes: 27 additions & 27 deletions src/server/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import {
AuthorizationError,
BackchannelLogoutError,
DiscoveryError,
FederatedConnectionAccessTokenErrorCode,
FederatedConnectionsAccessTokenError,
AccessTokenForConnectionError,
AccessTokenForConnectionErrorCode,
InvalidStateError,
MissingStateError,
OAuth2Error,
SdkError
} from "../errors";
import {
FederatedConnectionTokenSet,
GetFederatedConnectionAccessTokenOptions,
ConnectionTokenSet,
AccessTokenForConnectionOptions,
LogoutToken,
SessionData,
TokenSet
Expand Down Expand Up @@ -975,49 +975,49 @@ export class AuthClient {
}

/**
* Exchanges a refresh token for a federated connection access token.
* Exchanges a refresh token for an access token for a connection.
*
* This method performs a token exchange using the provided refresh token and connection details.
* It first checks if the refresh token is present in the `tokenSet`. If not, it returns an error.
* Then, it constructs the necessary parameters for the token exchange request and performs
* the request to the authorization server's token endpoint.
*
* @returns {Promise<[SdkError, null] | [null, FederatedConnectionTokenSet]>} A promise that resolves to a tuple.
* @returns {Promise<[SdkError, null] | [null, ConnectionTokenSet]>} A promise that resolves to a tuple.
* The first element is either an `SdkError` if an error occurred, or `null` if the request was successful.
* The second element is either `null` if an error occurred, or a `FederatedConnectionTokenSet` object
* The second element is either `null` if an error occurred, or a `ConnectionTokenSet` object
* containing the access token, expiration time, and scope if the request was successful.
*
* @throws {FederatedConnectionsAccessTokenError} If the refresh token is missing or if there is an error during the token exchange process.
* @throws {AccessTokenForConnectionError} If the refresh token is missing or if there is an error during the token exchange process.
*/
async getFederatedConnectionTokenSet(
async getConnectionTokenSet(
tokenSet: TokenSet,
federatedConnectionTokenSet: FederatedConnectionTokenSet | undefined,
options: GetFederatedConnectionAccessTokenOptions
): Promise<[SdkError, null] | [null, FederatedConnectionTokenSet]> {
connectionTokenSet: ConnectionTokenSet | undefined,
options: AccessTokenForConnectionOptions
): Promise<[SdkError, null] | [null, ConnectionTokenSet]> {
// If we do not have a refresh token
// and we do not have a federated connection token set in the cache or the one we have is expired,
// and we do not have a connection token set in the cache or the one we have is expired,
// there is noting to retrieve and we return an error.
if (
!tokenSet.refreshToken &&
(!federatedConnectionTokenSet ||
federatedConnectionTokenSet.expiresAt <= Date.now() / 1000)
(!connectionTokenSet ||
connectionTokenSet.expiresAt <= Date.now() / 1000)
) {
return [
new FederatedConnectionsAccessTokenError(
FederatedConnectionAccessTokenErrorCode.MISSING_REFRESH_TOKEN,
"A refresh token was not present, Federated Connection Access Token requires a refresh token. The user needs to re-authenticate."
new AccessTokenForConnectionError(
AccessTokenForConnectionErrorCode.MISSING_REFRESH_TOKEN,
"A refresh token was not present, Connection Access Token requires a refresh token. The user needs to re-authenticate."
),
null
];
}

// If we do have a refresh token,
// and we do not have a federated connection token set in the cache or the one we have is expired,
// we need to exchange the refresh token for a federated connection access token.
// and we do not have a connection token set in the cache or the one we have is expired,
// we need to exchange the refresh token for a connection access token.
if (
tokenSet.refreshToken &&
(!federatedConnectionTokenSet ||
federatedConnectionTokenSet.expiresAt <= Date.now() / 1000)
(!connectionTokenSet ||
connectionTokenSet.expiresAt <= Date.now() / 1000)
) {
const params = new URLSearchParams();

Expand Down Expand Up @@ -1063,9 +1063,9 @@ export class AuthClient {
} catch (err) {
console.error(err);
return [
new FederatedConnectionsAccessTokenError(
FederatedConnectionAccessTokenErrorCode.FAILED_TO_EXCHANGE,
"There was an error trying to exchange the refresh token for a federated connection access token. Check the server logs for more information."
new AccessTokenForConnectionError(
AccessTokenForConnectionErrorCode.FAILED_TO_EXCHANGE,
"There was an error trying to exchange the refresh token for a connection access token. Check the server logs for more information."
),
null
];
Expand All @@ -1084,9 +1084,9 @@ export class AuthClient {
];
}

return [null, federatedConnectionTokenSet] as [
return [null, connectionTokenSet] as [
null,
FederatedConnectionTokenSet
ConnectionTokenSet
];
}
}
Expand Down
Loading