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

Commit

Permalink
Merge pull request #131 from flexpa/feature/smart-name
Browse files Browse the repository at this point in the history
Improved route names for IdPs
  • Loading branch information
kodiakhq[bot] authored Nov 5, 2021
2 parents 90a1074 + 878c230 commit 815d5b7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @example Iterables returned for the search operation:
* ```typescript
* const { searche } = Client("https://r4.smarthealthit.org")
* const { search } = Client("https://r4.smarthealthit.org")
* const search = search("Patient");
* search.next();
* ```
Expand Down
13 changes: 12 additions & 1 deletion src/smart-auth/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,18 @@ describe("an authorize url", () => {
})
})


describe("a provider with a weird name", () => {
test("still has nice pretty URLs", async () => {
const response = await app.inject({
method: 'GET',
url: `/smart/bad-name/auth`
})

expect(response.statusCode).toBe(302)
expect(response.headers['location']).toBe("http://external.localhost/smart/oauth/authorize?response_type=code&client_id=123&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fsmart%2Fbad-name%2Fredirect&scope=launch&state=smart-auth-static-bytes-not-random-mock")
})
})

// @todo this requires implementing a mocked oauth server response
test.todo("a callback url (in the server fixture) is successfully callable")
//, async () => {
Expand Down
8 changes: 6 additions & 2 deletions src/smart-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,18 @@ function checkState(state: string) {
new Error('Invalid state')
}

function routeCase(value: string): string {
return value.toLowerCase().replace(/\s/g,'-');
}

const oauthPlugin: FastifyPluginCallback<SmartAuthProvider> = function (http, options, next) {
const { name, auth, client, scope: defaultScope, redirect } = options

const prefix = auth?.pathPrefix || "/smart";
const tokenHost = auth.tokenHost;
const authorizeParams = auth?.authorizeParams || {}
const authorizeRedirectPath = `${prefix}/${name.toLowerCase()}/auth`
const redirectPath = redirect.path || `${prefix}/${name.toLowerCase()}/redirect`
const authorizeRedirectPath = `${prefix}/${routeCase(name)}/auth`
const redirectPath = redirect.path || `${prefix}/${routeCase(name)}/redirect`
const redirectUri = `${redirect.host}${redirectPath}`

function generateAuthorizationUri(scope?: SmartAuthScope[]) {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/authServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import smartAuthProviderExample from "../smart-auth/idp";
import { smartAuthProviderExample, badNameProviderExample } from "../smart-auth/idp";
import Fastify from "fastify";
import {
SmartAuth,
Expand All @@ -16,6 +16,7 @@ declare module 'fastify' {
export const fastify = Fastify();

fastify.register(SmartAuth, { prefix: '/smart', ...smartAuthProviderExample })
fastify.register(SmartAuth, { prefix: '/smart', ...badNameProviderExample })

fastify.get<Querystring>("/smart/idp/redirect", QuerystringAjvSchema, async function (request, reply) {
const response = await this.idp.getAccessTokenFromAuthorizationCodeFlow(request)
Expand Down
19 changes: 17 additions & 2 deletions test/smart-auth/idp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SmartAuthProvider } from "../../src";

const smartAuthProviderExample: SmartAuthProvider = {
export const smartAuthProviderExample: SmartAuthProvider = {
name: "idp",
scope: ["launch"],
client: {
Expand All @@ -17,4 +17,19 @@ const smartAuthProviderExample: SmartAuthProvider = {
iss: "http://external.localhost/issuer",
};

export default smartAuthProviderExample;
export const badNameProviderExample: SmartAuthProvider = {
name: "Bad Name",
scope: ["launch"],
client: {
id: "123",
secret: "somesecret",
},
auth: {
tokenHost: "http://external.localhost",
authorizePath: "/smart/oauth/authorize",
},
redirect: {
host: "http://localhost:3000",
},
iss: "http://external.localhost/issuer",
};

0 comments on commit 815d5b7

Please sign in to comment.