-
Notifications
You must be signed in to change notification settings - Fork 130
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
Draft: OIDC native login #660
base: master
Are you sure you want to change the base?
Changes from all commits
a59e4bd
5379b83
24c5879
2604e6a
b9bca9d
83684c8
2ba8bc3
68daf51
dd8cd31
21cf845
46e884b
4644004
8da49df
f976430
0a4822c
06a2068
f31f57e
786a082
7463145
a44f13e
36050b1
f8dca77
485e8a2
1ea9eda
35bb265
462b8b6
7dc30c4
6de66f7
9b159a7
9fd7f25
c8bff10
49d1547
17875e4
37e9727
1ead9bc
f177a94
a4c16e5
896f2b7
9ce9e2d
a2370da
7c40c7c
b4ff736
13a4299
9c52fb9
317d97c
1716a30
94352da
f4b1d99
59b06a0
2739572
da86db3
0aafd55
6580fcf
7b7557a
97b8861
d6dff1d
6acc0ea
bde85c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2021 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import {OidcApi} from "../../matrix/net/OidcApi"; | ||
import {ViewModel} from "../ViewModel"; | ||
import {OIDCLoginMethod} from "../../matrix/login/OIDCLoginMethod"; | ||
import {LoginFailure} from "../../matrix/Client"; | ||
|
||
export class CompleteOIDCLoginViewModel extends ViewModel { | ||
constructor(options) { | ||
super(options); | ||
const { | ||
state, | ||
code, | ||
attemptLogin, | ||
} = 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; | ||
this._errorMessage = ""; | ||
this.performOIDCLoginCompletion(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally we try (there is some places where we sin though, like LoginViewModel) to not call async methods from the constructor (which can't be async itself), unless there is really no other way and we can be 100% sure the method wont throw. Usually, we deal with this by adding an async |
||
} | ||
|
||
get errorMessage() { return this._errorMessage; } | ||
|
||
_showError(message) { | ||
this._errorMessage = message; | ||
this.emitChange("errorMessage"); | ||
} | ||
|
||
async performOIDCLoginCompletion() { | ||
if (!this._state || !this._code) { | ||
return; | ||
} | ||
const code = this._code; | ||
// TODO: cleanup settings storage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment still relevant? |
||
const [startedAt, nonce, codeVerifier, redirectUri, homeserver, issuer, clientId, accountManagementUrl] = await Promise.all([ | ||
this.platform.settingsStorage.getInt(`oidc_${this._state}_started_at`), | ||
this.platform.settingsStorage.getString(`oidc_${this._state}_nonce`), | ||
this.platform.settingsStorage.getString(`oidc_${this._state}_code_verifier`), | ||
this.platform.settingsStorage.getString(`oidc_${this._state}_redirect_uri`), | ||
this.platform.settingsStorage.getString(`oidc_${this._state}_homeserver`), | ||
this.platform.settingsStorage.getString(`oidc_${this._state}_issuer`), | ||
this.platform.settingsStorage.getString(`oidc_${this._state}_client_id`), | ||
this.platform.settingsStorage.getString(`oidc_${this._state}_account_management_url`), | ||
]); | ||
|
||
const oidcApi = new OidcApi({ | ||
issuer, | ||
clientId, | ||
request: this._request, | ||
encoding: this._encoding, | ||
crypto: this._crypto, | ||
}); | ||
const method = new OIDCLoginMethod({oidcApi, nonce, codeVerifier, code, homeserver, startedAt, redirectUri, accountManagementUrl}); | ||
const status = await this._attemptLogin(method); | ||
let error = ""; | ||
switch (status) { | ||
case LoginFailure.Credentials: | ||
error = this.i18n`Your login token is invalid.`; | ||
break; | ||
case LoginFailure.Connection: | ||
error = this.i18n`Can't connect to ${homeserver}.`; | ||
break; | ||
case LoginFailure.Unknown: | ||
error = this.i18n`Something went wrong while checking your login token.`; | ||
break; | ||
} | ||
if (error) { | ||
this._showError(error); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: In view models, there no need to store the properties of the options in member variables as the options are stored in the ViewModel base class. In this case, you can just do
this.platform.crypto
/encoding/request at any point in a view model.