Skip to content

Commit

Permalink
Use platform APIs for text encoding and hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Mar 18, 2022
1 parent 762dc0a commit 9889234
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/domain/login/CompleteOIDCLoginViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class CompleteOIDCLoginViewModel extends ViewModel {
} = options;
this._request = options.platform.request;
this._encoding = options.platform.encoding;
this._crypto = options.platform.crypto;
this._state = state;
this._code = code;
this._attemptLogin = attemptLogin;
Expand Down Expand Up @@ -63,6 +64,7 @@ export class CompleteOIDCLoginViewModel extends ViewModel {
clientId: "hydrogen-web",
request: this._request,
encoding: this._encoding,
crypto: this._crypto,
});
const method = new OIDCLoginMethod({oidcApi, nonce, codeVerifier, code, homeserver, startedAt, redirectUri});
const status = await this._attemptLogin(method);
Expand Down
1 change: 1 addition & 0 deletions src/domain/login/StartOIDCLoginViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class StartOIDCLoginViewModel extends ViewModel {
issuer: this._issuer,
request: this.platform.request,
encoding: this.platform.encoding,
crypto: this.platform.crypto,
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/matrix/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class Client {
clientId: "hydrogen-web",
request: this._platform.request,
encoding: this._platform.encoding,
crypto: this._platform.crypto,
});
await oidcApi.validate();

Expand Down Expand Up @@ -264,6 +265,7 @@ export class Client {
clientId: "hydrogen-web",
request: this._platform.request,
encoding: this._platform.encoding,
crypto: this._platform.crypto,
});

// TODO: stop/pause the refresher?
Expand Down
15 changes: 8 additions & 7 deletions src/matrix/net/OidcApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ export class OidcApi {
_issuer: string;
_clientId: string;
_requestFn: any;
_base64: any;
_encoding: any;
_crypto: any;
_metadataPromise: Promise<any>;

constructor({ issuer, clientId, request, encoding }) {
constructor({ issuer, clientId, request, encoding, crypto }) {
this._issuer = issuer;
this._clientId = clientId;
this._requestFn = request;
this._base64 = encoding.base64;
this._encoding = encoding;
this._crypto = crypto;
}

get metadataUrl() {
Expand Down Expand Up @@ -110,10 +112,9 @@ export class OidcApi {
async _generateCodeChallenge(
codeVerifier: string
): Promise<string> {
const encoder = new TextEncoder();
const data = encoder.encode(codeVerifier);
const digest = await window.crypto.subtle.digest("SHA-256", data);
const base64Digest = this._base64.encode(digest);
const data = this._encoding.utf8.encode(codeVerifier);
const digest = await this._crypto.digest("SHA-256", data);
const base64Digest = this._encoding.base64.encode(digest);
return base64Digest.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}

Expand Down

0 comments on commit 9889234

Please sign in to comment.