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

FEAT: Federated connections #1879

Open
wants to merge 15 commits into
base: v4-backup
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
43 changes: 43 additions & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,46 @@ export class AccessTokenError extends SdkError {
this.code = code
}
}

/**
* Enum representing error codes related to federated connection access tokens.
*/
export enum FederatedConnectionAccessTokenErrorCode {
/**
* The session is missing.
*/
MISSING_SESSION = "missing_session",

/**
* The refresh token is missing.
*/
MISSING_REFRESH_TOKEN = "missing_refresh_token",

/**
* Failed to exchange the refresh token.
*/
FAILED_TO_EXCHANGE = "failed_to_exchange_refresh_token",
}

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

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

describe("federatedConnectionAccessToken", async () => {

it("Should exchange a refresh token for an access token", async () => {
const secret = await generateSecret(32)
const transactionStore = new TransactionStore({
secret,
})
const sessionStore = new StatelessSessionStore({
secret,
})
const authClient = new AuthClient({
transactionStore,
sessionStore,

domain: DEFAULT.domain,
clientId: DEFAULT.clientId,
clientSecret: DEFAULT.clientSecret,

secret,
appBaseUrl: DEFAULT.appBaseUrl,

fetch: getMockAuthorizationServer({
tokenEndpointResponse: {
token_type: "Bearer",
access_token: DEFAULT.accessToken,
expires_in: 86400, // expires in 10 days
} as oauth.TokenEndpointResponse,
}),
})

const expiresAt = Math.floor(Date.now() / 1000) - 10 * 24 * 60 * 60 // expired 10 days ago
const tokenSet = {
accessToken: DEFAULT.accessToken,
refreshToken: DEFAULT.refreshToken,
expiresAt,
};

const response = await authClient.federatedConnectionTokenExchange(tokenSet, "google-oauth2", "000100123");
const [error, federatedConnectionTokenSet] = response;
expect(error).toBe(null);
expect(federatedConnectionTokenSet).toEqual({
accessToken: DEFAULT.accessToken,
expiresAt: expect.any(Number),
})
})

it("should return an error if the discovery endpoint could not be fetched", async () => {
const secret = await generateSecret(32)
const transactionStore = new TransactionStore({
secret,
})
const sessionStore = new StatelessSessionStore({
secret,
})
const authClient = new AuthClient({
transactionStore,
sessionStore,

domain: DEFAULT.domain,
clientId: DEFAULT.clientId,
clientSecret: DEFAULT.clientSecret,

secret,
appBaseUrl: DEFAULT.appBaseUrl,

fetch: getMockAuthorizationServer({
discoveryResponse: new Response(null, { status: 500 }),
}),
})

const expiresAt = Math.floor(Date.now() / 1000) - 10 * 24 * 60 * 60 // expired 10 days ago
const tokenSet = {
accessToken: DEFAULT.accessToken,
refreshToken: DEFAULT.refreshToken,
expiresAt,
}

const [error, federatedConnectionTokenSet] = await authClient.federatedConnectionTokenExchange(tokenSet, "google-oauth2")
expect(error?.code).toEqual("discovery_error")
expect(federatedConnectionTokenSet).toBeNull()
})

it("should return an error if the token set does not contain a refresh token", async () => {
const secret = await generateSecret(32)
const transactionStore = new TransactionStore({
secret,
})
const sessionStore = new StatelessSessionStore({
secret,
})
const authClient = new AuthClient({
transactionStore,
sessionStore,

domain: DEFAULT.domain,
clientId: DEFAULT.clientId,
clientSecret: DEFAULT.clientSecret,

secret,
appBaseUrl: DEFAULT.appBaseUrl,

fetch: getMockAuthorizationServer(),
})

const expiresAt = Math.floor(Date.now() / 1000) - 10 * 24 * 60 * 60 // expired 10 days ago
const tokenSet = {
accessToken: DEFAULT.accessToken,
expiresAt,
}

const [error, federatedConnectionTokenSet] = await authClient.federatedConnectionTokenExchange(tokenSet, "google-oauth2")
expect(error?.code).toEqual("missing_refresh_token")
expect(federatedConnectionTokenSet).toBeNull()
})
});
})

const _authorizationServerMetadata = {
Expand Down
Loading