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

add convertStringExpiresInToNumber function for providers whose response's expires_in is a string instead of an integer(number) #112

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 28 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,24 @@ function isJsonObject<T = JsonObject>(input: unknown): input is T {
return true
}

/**
* Some providers(like Naver) do not adhere to OAuth 2.0 specs, "expires_in" is not an integer but an string(not 3600 but "3600").
* If a response(json) has "expires_in" field and its type is string, this function will convert it to a number.
* @author hotsixman(https://github.com/Hotsixisbest)
*
* @param json
* @returns json
*/
function convertStringExpiresInToNumber(json:JsonValue|null){
if(json !== null && typeof json === "object" && !Array.isArray(json) && json.expires_in !== undefined && typeof json.expires_in === 'string'){
let expiresIn = Number(json.expires_in);
if(!isNaN(expiresIn)){
json.expires_in = expiresIn
}
}
return json;
}

function prepareHeaders(input?: [string, string][] | Record<string, string> | Headers): Headers {
if (looseInstanceOf(input, Headers)) {
input = Object.fromEntries(input.entries())
Expand Down Expand Up @@ -1990,6 +2008,8 @@ export async function processPushedAuthorizationResponse(
throw new OPE('failed to parse "response" body as JSON', { cause })
}

json = convertStringExpiresInToNumber(json);

if (!isJsonObject<PushedAuthorizationResponse>(json)) {
throw new OPE('"response" body must be a top level object')
}
Expand Down Expand Up @@ -2538,32 +2558,34 @@ async function processGenericAccessTokenResponse(
throw new OPE('failed to parse "response" body as JSON', { cause })
}

json = convertStringExpiresInToNumber(json);

if (!isJsonObject<TokenEndpointResponse>(json)) {
throw new OPE('"response" body must be a top level object')
}

if (!validateString(json.access_token)) {
throw new OPE('"response" body "access_token" property must be a non-empty string')
}

if (!validateString(json.token_type)) {
throw new OPE('"response" body "token_type" property must be a non-empty string')
}

// @ts-expect-error
json.token_type = json.token_type.toLowerCase()

if (json.token_type !== 'dpop' && json.token_type !== 'bearer') {
throw new UnsupportedOperationError('unsupported `token_type` value')
}

if (
json.expires_in !== undefined &&
(typeof json.expires_in !== 'number' || json.expires_in <= 0)
) {
throw new OPE('"response" body "expires_in" property must be a positive number')
}

if (
!ignoreRefreshToken &&
json.refresh_token !== undefined &&
Expand Down Expand Up @@ -4133,6 +4155,8 @@ export async function processDeviceAuthorizationResponse(
throw new OPE('failed to parse "response" body as JSON', { cause })
}

json = convertStringExpiresInToNumber(json);

if (!isJsonObject<DeviceAuthorizationResponse>(json)) {
throw new OPE('"response" body must be a top level object')
}
Expand Down