Skip to content

feature/conditionally update session handleAccessToken #2054

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
38 changes: 20 additions & 18 deletions src/server/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import packageJson from "../../package.json";
import {
AccessTokenError,
AccessTokenErrorCode,
AccessTokenForConnectionError,
AccessTokenForConnectionErrorCode,
AuthorizationCodeGrantError,
AuthorizationError,
BackchannelLogoutError,
DiscoveryError,
AccessTokenForConnectionError,
AccessTokenForConnectionErrorCode,
InvalidStateError,
MissingStateError,
OAuth2Error,
SdkError
} from "../errors";
import {
AccessTokenForConnectionOptions,
AuthorizationParameters,
ConnectionTokenSet,
AccessTokenForConnectionOptions,
LogoutToken,
SessionData,
StartInteractiveLoginOptions,
Expand Down Expand Up @@ -65,7 +65,6 @@ const DEFAULT_SCOPES = ["openid", "profile", "email", "offline_access"].join(
" "
);


/**
* A constant representing the grant type for federated connection access token exchange.
*
Expand Down Expand Up @@ -616,10 +615,16 @@ export class AuthClient {
expires_at: updatedTokenSet.expiresAt
});

await this.sessionStore.set(req.cookies, res.cookies, {
...session,
tokenSet: updatedTokenSet
});
if (
updatedTokenSet.accessToken !== session.tokenSet.accessToken ||
updatedTokenSet.refreshToken !== session.tokenSet.refreshToken ||
updatedTokenSet.expiresAt !== session.tokenSet.expiresAt
) {
await this.sessionStore.set(req.cookies, res.cookies, {
...session,
tokenSet: updatedTokenSet
});
}

return res;
}
Expand Down Expand Up @@ -1016,19 +1021,20 @@ export class AuthClient {
tokenSet: TokenSet,
connectionTokenSet: ConnectionTokenSet | undefined,
options: AccessTokenForConnectionOptions
): Promise<[AccessTokenForConnectionError, null] | [null, ConnectionTokenSet]> {
): Promise<
[AccessTokenForConnectionError, null] | [null, ConnectionTokenSet]
> {
// If we do not have a refresh token
// 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 &&
(!connectionTokenSet ||
connectionTokenSet.expiresAt <= Date.now() / 1000)
(!connectionTokenSet || connectionTokenSet.expiresAt <= Date.now() / 1000)
) {
return [
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.",
"A refresh token was not present, Connection Access Token requires a refresh token. The user needs to re-authenticate."
),
null
];
Expand All @@ -1039,8 +1045,7 @@ export class AuthClient {
// we need to exchange the refresh token for a connection access token.
if (
tokenSet.refreshToken &&
(!connectionTokenSet ||
connectionTokenSet.expiresAt <= Date.now() / 1000)
(!connectionTokenSet || connectionTokenSet.expiresAt <= Date.now() / 1000)
) {
const params = new URLSearchParams();

Expand Down Expand Up @@ -1111,10 +1116,7 @@ export class AuthClient {
];
}

return [null, connectionTokenSet] as [
null,
ConnectionTokenSet
];
return [null, connectionTokenSet] as [null, ConnectionTokenSet];
}
}

Expand Down