Skip to content
This repository has been archived by the owner on Jun 13, 2022. It is now read-only.

fix: update getAccessTokenFromClientCredentialFlow to take scope as a param #168

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/smart-auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Sero will pass your scoped parameters in its generated authorization URL for you
* `SmartAuthRedirectQuerystring` is a TS interface that types the Fastify route contraints for the redirect/callback url - see example for use
* `SmartAuthUrlQuerystring` is a TS interface that types the Fastify route contraints for the auto-generated authorization URL starting point described above. You can customize scopes on a per request basis.
* `SmartAuthRedirectQuerystringSchema` is the AJV schema definition that corresponds to `SmartAuthRedirectQuerystring`, and can be used in the Fastify runtime - see example for use
* `getAccessTokenFromClientCredentialFlow` is a function to fetch a `client_credential` access token for a given `SmartAuthProvider`. It will also prioritize the passed in `scope: string[]` over the `smartAuthProvider.scope`, in case you need special scope(s) for this flow. This function is not decorated on the fastify server, so it can be called directly on a `SmartAuthProvider`.

### Decorators

Expand Down
1 change: 0 additions & 1 deletion src/smart-auth/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ describe("getAccessTokenFromClientCredentialFlow", () => {
},
auth: {
tokenHost: 'http://localhost/token',
clientCredentialsScope: ['Public NonPII'],
},
redirect: {
host: 'http://localhost:3000/smart/smart-stub/auth',
Expand Down
6 changes: 3 additions & 3 deletions src/smart-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export type SmartAuthProvider = {
tokenPath?: string;
/** String path to revoke an access token. Default to /oauth/revoke. */
revokePath?: string;
/** Overrides for client credentials */
clientCredentialsScope?: (SmartAuthScope | string)[];
};
redirect: {
/** A required host name for the auth code exchange redirect path. */
Expand All @@ -69,6 +67,7 @@ export interface SmartAuthNamespace {

getAccessTokenFromClientCredentialFlow(
smartAuthProvider: SmartAuthProvider,
scope?: string[],
): Promise<AccessToken>;

getNewAccessTokenUsingRefreshToken(
Expand Down Expand Up @@ -208,6 +207,7 @@ const oauthPlugin: FastifyPluginCallback<SmartAuthProvider> = function (http, op

export const getAccessTokenFromClientCredentialFlow = async (
smartAuthProvider: SmartAuthProvider,
scope?: string[]
): Promise<AccessToken | undefined> => {
const clientCredentialsOptions = {
client: smartAuthProvider.client,
Expand All @@ -219,7 +219,7 @@ export const getAccessTokenFromClientCredentialFlow = async (

const client = new ClientCredentials(clientCredentialsOptions);
const tokenParams = {
scope: smartAuthProvider.auth?.clientCredentialsScope || smartAuthProvider.scope,
scope: scope || smartAuthProvider.scope,
};

try {
Expand Down