Skip to content

Add support for extra parameters in refresh requests #11

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

Merged
merged 3 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Token Handler Assistant Changelog

## [Pending]

- Add support for extra parameters in refresh requests

## [1.1.0] - 2024-08-12

- Send `token-handler-version` header in all requests
Expand Down
16 changes: 10 additions & 6 deletions src/oauth-agent-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
EndLoginRequest,
LogoutResponse,
OAuthAgentRemoteError,
RefreshRequest,
RefreshResponse,
SessionResponse,
StartLoginRequest,
Expand Down Expand Up @@ -48,13 +49,16 @@ export class OAuthAgentClient {

/**
* Refreshes the access token. Calls the `/refresh` endpoint.
*
* @param request the refresh request possibly containing extra parameters
*
* @return the refresh token response possibly containing the new access token's expiration time
*
*
* @throws OAuthAgentRemoteError when OAuth Agent responded with an error
*/
async refresh(): Promise<RefreshResponse> {
const refreshResponse = await this.fetch("POST", "refresh")
async refresh(request?: RefreshRequest): Promise<RefreshResponse> {
const urlSearchParams = this.toUrlSearchParams(request?.extraRefreshParameters)
const refreshResponse = await this.fetch("POST", "refresh", urlSearchParams)

return {
accessTokenExpiresIn: refreshResponse.access_token_expires_in
Expand Down Expand Up @@ -162,20 +166,20 @@ export class OAuthAgentClient {

}

private toUrlSearchParams(data: {[key: string]: string; } | undefined): URLSearchParams {
private toUrlSearchParams(data: { [key: string]: string; } | undefined): URLSearchParams {
if (!data) {
return new URLSearchParams()
}
return new URLSearchParams(data)
}

private async fetch(method: string, path: string, content?: URLSearchParams): Promise<any> {
const headers= {
const headers = {
accept: 'application/json',
'token-handler-version': '1'
} as Record<string, string>

if (path == 'login/start' || path == 'login/end') {
if (content && content.size !== 0) {
headers["content-type"] = 'application/x-www-form-urlencoded'
}

Expand Down
12 changes: 11 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* (such as `scope`, `login_hint` or `ui_locales`). These parameters will be used in the authorization request.
* Each parameter has to be explicitly allowed in the configuration of the token handler application
* in the Curity server.
*
*/
export interface StartLoginRequest {
readonly extraAuthorizationParameters?: { [key: string]: string };
Expand Down Expand Up @@ -54,6 +53,17 @@ export interface SessionResponse {
readonly accessTokenExpiresIn?: number;
}

/**
* Passed to {@link OAuthAgentClient#refresh} function.
*/
export interface RefreshRequest {
/**
* Extra parameters to be used in the token refresh request.
* Each parameter has to be explicitly allowed in the configuration of the token handler application
* in the Curity server.
*/
readonly extraRefreshParameters?: { [key: string]: string };
}

/**
* Returned from the {@link OAuthAgentClient#refresh} function. Contains:
Expand Down