Skip to content

Commit

Permalink
Remove deprecated domain usage and add provider support
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeviant committed Oct 22, 2022
1 parent 87b3ff1 commit 360adf7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 65 deletions.
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,8 @@ app.get(

### Login

The login route will redirect to a [WorkOS OAuth 2.0 authorization URL](https://workos.com/docs/reference/sso/authorize/get). When redirecting to this route, be sure to include one of the [supported query parameters](https://workos.com/docs/reference/sso/authorize/get#authorize-get-parameters)

> **Note**
> An additional `email` query parameter is supported which will extract the `domain` and forward it to WorkOS
**Example**

```
location.href = "/auth/workos/login?domain=gmail.com"
```
The login route will redirect to a [WorkOS OAuth 2.0 authorization URL](https://workos.com/docs/reference/sso/get-authorization-url). When redirecting to this route, be sure to include one of the [supported query parameters](https://workos.com/docs/reference/sso/get-authorization-url)

### Callback

This will be called by WorkOS after a successful login. Be sure to [configure the redirect URI](https://workos.com/docs/sso/guide/set-redirect-uri) with WorkOS.
This will be called by WorkOS after a successful login. Be sure to [configure the redirect URI](https://workos.com/docs/reference/sso/redirect-uri) with WorkOS.
98 changes: 50 additions & 48 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConnectionType } from "@workos-inc/node";
import express from "express";
import expressSession from "express-session";
import passport from "passport";
import supertest from "supertest";
import { WorkOSSSOStrategy } from "./";
Expand Down Expand Up @@ -108,55 +108,57 @@ describe("on login", () => {
});
});

describe("on domain", () => {
const domain = "mydomain.org";
const url = `/workos/authorize?domain=${domain}`;

it("calls workos api with domain", async () => {
await supertest(app).get(url);
expect(getAuthorizationURL).toBeCalledTimes(1);
expect(getAuthorizationURL).toBeCalledWith(
expect.objectContaining({
domain,
clientID,
redirectURI: callbackURL,
state: "...",
})
);
describe("on provider", () => {
describe("with 'GoogleOAuth'", () => {
const provider = ConnectionType.GoogleOAuth;
const url = `/workos/authorize?provider=${provider}`;

it("calls workos api with provider", async () => {
await supertest(app).get(url);
expect(getAuthorizationURL).toBeCalledTimes(1);
expect(getAuthorizationURL).toBeCalledWith(
expect.objectContaining({
provider,
clientID,
redirectURI: callbackURL,
state: "...",
})
);
});

it("redirects to login url", async () => {
const res = await supertest(app).get(url);
expect(res.statusCode).toEqual(302);
expect(res.headers.location).toMatchInlineSnapshot(
`"https://workos.com/fake-auth-url"`
);
});
});

it("redirects to login url", async () => {
const res = await supertest(app).get(url);
expect(res.statusCode).toEqual(302);
expect(res.headers.location).toMatchInlineSnapshot(
`"https://workos.com/fake-auth-url"`
);
});
});

describe("on email", () => {
const email = "user@mydomain.org";
const url = `/workos/authorize?email=${email}`;

it("calls workos api with domain", async () => {
await supertest(app).get(url);
expect(getAuthorizationURL).toBeCalledTimes(1);
expect(getAuthorizationURL).toBeCalledWith(
expect.objectContaining({
domain: email.substring(email.indexOf("@") + 1),
clientID,
redirectURI: callbackURL,
state: "...",
})
);
});

it("redirects to login url", async () => {
const res = await supertest(app).get(url);
expect(res.statusCode).toEqual(302);
expect(res.headers.location).toMatchInlineSnapshot(
`"https://workos.com/fake-auth-url"`
);
describe("with 'MicrosoftOAuth'", () => {
const provider = ConnectionType.MicrosoftOAuth;
const url = `/workos/authorize?provider=${provider}`;

it("calls workos api with provider", async () => {
await supertest(app).get(url);
expect(getAuthorizationURL).toBeCalledTimes(1);
expect(getAuthorizationURL).toBeCalledWith(
expect.objectContaining({
provider,
clientID,
redirectURI: callbackURL,
state: "...",
})
);
});

it("redirects to login url", async () => {
const res = await supertest(app).get(url);
expect(res.statusCode).toEqual(302);
expect(res.headers.location).toMatchInlineSnapshot(
`"https://workos.com/fake-auth-url"`
);
});
});
});
});
Expand Down
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,21 @@ export class WorkOSSSOStrategy extends Strategy {

private _loginAttempt(req: Request, options: AuthenticateOptions) {
try {
const { connection, domain, email, organization } = req.query as Record<
const { connection, organization, provider } = req.query as Record<
string,
string
>;
if (
[connection, domain, email, organization].every((a) => a === undefined)
) {
if ([connection, organization, provider].every((a) => a === undefined)) {
throw Error(
"One of 'connection', 'domain', 'organization' and/or 'email' are required"
"One of 'connection', 'organization', or 'provider' is required"
);
}

const url = this.client.sso.getAuthorizationURL({
...req.body,
connection,
organization,
domain: domain || email?.slice(email.indexOf("@") + 1),
provider,
clientID: this.options.clientID,
redirectURI: options.redirectURI || this.options.callbackURL,
...options,
Expand Down

0 comments on commit 360adf7

Please sign in to comment.