Skip to content

Commit da16494

Browse files
committed
feat: support JWT response on userinfo endpoint
1 parent 8d152c2 commit da16494

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

projects/lib/src/oauth-service.ts

+46-27
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
694694
userName: string,
695695
password: string,
696696
headers: HttpHeaders = new HttpHeaders()
697-
): Promise<UserInfo> {
697+
): Promise<UserInfo | string> {
698698
return this.fetchTokenUsingPasswordFlow(
699699
userName,
700700
password,
@@ -708,7 +708,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
708708
* When using this with OAuth2 password flow, make sure that the property oidc is set to false.
709709
* Otherwise stricter validations take place that make this operation fail.
710710
*/
711-
public loadUserProfile(): Promise<UserInfo> {
711+
public loadUserProfile(): Promise<UserInfo | string> {
712712
if (!this.hasValidAccessToken()) {
713713
throw new Error('Can not load User Profile without access_token');
714714
}
@@ -725,35 +725,54 @@ export class OAuthService extends AuthConfig implements OnDestroy {
725725
);
726726

727727
this.http
728-
.get<UserInfo>(this.userinfoEndpoint, { headers })
728+
.get(this.userinfoEndpoint, {
729+
headers,
730+
observe: 'response',
731+
responseType: 'text'
732+
})
729733
.subscribe(
730-
info => {
731-
this.debug('userinfo received', info);
732-
733-
const existingClaims = this.getIdentityClaims() || {};
734-
735-
if (!this.skipSubjectCheck) {
736-
if (
737-
this.oidc &&
738-
(!existingClaims['sub'] || info.sub !== existingClaims['sub'])
739-
) {
740-
const err =
741-
'if property oidc is true, the received user-id (sub) has to be the user-id ' +
742-
'of the user that has logged in with oidc.\n' +
743-
'if you are not using oidc but just oauth2 password flow set oidc to false';
744-
745-
reject(err);
746-
return;
734+
response => {
735+
this.debug('userinfo received', JSON.stringify(response));
736+
if (
737+
response.headers
738+
.get('content-type')
739+
.startsWith('application/json')
740+
) {
741+
let info = response.body;
742+
const existingClaims = this.getIdentityClaims() || {};
743+
744+
if (!this.skipSubjectCheck) {
745+
if (
746+
this.oidc &&
747+
(!existingClaims['sub'] || info.sub !== existingClaims['sub'])
748+
) {
749+
const err =
750+
'if property oidc is true, the received user-id (sub) has to be the user-id ' +
751+
'of the user that has logged in with oidc.\n' +
752+
'if you are not using oidc but just oauth2 password flow set oidc to false';
753+
754+
reject(err);
755+
return;
756+
}
747757
}
748-
}
749758

750-
info = Object.assign({}, existingClaims, info);
759+
info = Object.assign({}, existingClaims, info);
751760

752-
this._storage.setItem('id_token_claims_obj', JSON.stringify(info));
753-
this.eventsSubject.next(
754-
new OAuthSuccessEvent('user_profile_loaded')
755-
);
756-
resolve(info);
761+
this._storage.setItem(
762+
'id_token_claims_obj',
763+
JSON.stringify(info)
764+
);
765+
this.eventsSubject.next(
766+
new OAuthSuccessEvent('user_profile_loaded')
767+
);
768+
resolve(info);
769+
} else {
770+
this.debug('userinfo is not JSON, treating it as JWE/JWS');
771+
this.eventsSubject.next(
772+
new OAuthSuccessEvent('user_profile_loaded')
773+
);
774+
resolve(response.body);
775+
}
757776
},
758777
err => {
759778
this.logger.error('error loading user info', err);

0 commit comments

Comments
 (0)