oidc-client-ts
oidc-client-ts copied to clipboard
UserManager support Password Credentials Grant
Password Credentials Grant https://www.rfc-editor.org/rfc/rfc6749#section-4.3
Just a post
POST https://keycloak/auth/realms/demo/protocol/openid-connect/token
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=admin&password=admin&client_id=demo-web
A workaround
class ClientAuthUserManager extends UserManager {
async signinCredential({ username, password }: { username: string; password: string }) {
let logger = this._logger.create('signinCredential');
let url = await this.metadataService.getTokenEndpoint();
if (!url) {
throw new Error(``);
}
let resp;
try {
let r = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
username,
password,
grant_type: 'password',
client_id: this.settings.client_id,
}).toString(),
mode: 'cors',
});
resp = await r.json();
} catch (e) {
logger.error(`Login failed`, e);
throw new Error('Login failed');
}
let user = new User(resp);
await this.storeUser(user);
logger.debug('user stored');
this._events.load(user);
return user;
}
}
Thanks for sharing, i would accept a MR, which adds this feature. Most of the part can be achieved with JsonService.post i guess...
@wenerme :
There is another similar issue from me (https://github.com/authts/oidc-client-ts/issues/746) and a PR (https://github.com/authts/oidc-client-ts/pull/747). Feel free to comment if it does not fit your needs and I'll do my best.
I think we should close this issue.