Skip to content

Commit

Permalink
Added AuthProvider.getDisplayName
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Jan 19, 2024
1 parent 78e8abe commit 45c74cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Support to retrieve conformance classes
- Added `Connection.makeLinksAbsolute`
- Added `AuthProvider.getDisplayName`
- Hook to migrate capabilities
- Support for `usage`, `log_level`, `links` in batch jobs and services
- Support for `level` parameter for log requests in batch jobs and services
Expand Down
9 changes: 9 additions & 0 deletions src/authprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ class AuthProvider {
return id;
}

/**
* Returns a display name for the authenticated user.
*
* @returns {string?} Name of the user or `null`
*/
getDisplayName() {
return null;
}

/**
* Returns the type of the authentication procedure as specified by the API, e.g. `oidc` or `basic`.
*
Expand Down
21 changes: 21 additions & 0 deletions src/basicprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class BasicProvider extends AuthProvider {
title: "HTTP Basic",
description: "Login with username and password using the method HTTP Basic."
});
this.username = null;
}

/**
Expand All @@ -41,9 +42,29 @@ class BasicProvider extends AuthProvider {
if (!Utils.isObject(response.data) || typeof response.data.access_token !== 'string') {
throw new Error("No access_token returned.");
}
this.username = username;
this.setToken(response.data.access_token);
}

/**
* Returns a display name for the authenticated user.
*
* @returns {string?} Name of the user or `null`
*/
getDisplayName() {
return this.username;
}

/**
* Logout from the established session.
*
* @async
*/
async logout() {
this.username = null;
await super.logout();
}

}

module.exports = BasicProvider;
12 changes: 12 additions & 0 deletions src/oidcprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@ class OidcProvider extends AuthProvider {
}
}

/**
* Returns a display name for the authenticated user.
*
* @returns {string?} Name of the user or `null`
*/
getDisplayName() {
if (this.user && Utils.isObject(this.user.profile)) {
return this.user.profile.name || this.user.profile.preferred_username || this.user.profile.email || null;
}
return null;
}

/**
* Detects the default OIDC client ID for the given redirect URL.
*
Expand Down

0 comments on commit 45c74cf

Please sign in to comment.