Skip to content

fix(apple-sign-in): SHA256 hash nonce in request #241

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 1 commit into from
Feb 27, 2022
Merged
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
13 changes: 11 additions & 2 deletions packages/apple-sign-in/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ function randomNonce(length: number) {
return result;
}

function sha256Hash(input: string): string {
const sha256buffer = CC_SHA256(interop.handleof(NSString.stringWithString(input).UTF8String), input.length, interop.alloc(input.length));
let inputHashed = '';
for (let i = 0; i < input.length; i++) {
inputHashed += parseInt(sha256buffer[i], 10).toString(16).padStart(2, '0');
}
return inputHashed;
}

export class SignIn {
static #controller: ASAuthorizationController;
static #delegate: ASAuthorizationControllerDelegate;
Expand Down Expand Up @@ -100,10 +109,10 @@ export class SignIn {

if (options?.useNonce) {
if (options.nonce) {
request.nonce = options.nonce;
request.nonce = sha256Hash(options.nonce);
} else {
const nonce = randomNonce(32);
request.nonce = nonce;
request.nonce = sha256Hash(nonce);
(this.#delegate as any)._options.nonce = nonce;
}
}
Expand Down