Skip to content

Commit

Permalink
Add support for providers
Browse files Browse the repository at this point in the history
  • Loading branch information
andyrichardson committed Nov 2, 2022
1 parent 360adf7 commit 9888b32
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
52 changes: 52 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,58 @@ 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: "...",
})
);
});

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("on provider", () => {
describe("with 'GoogleOAuth'", () => {
const provider = ConnectionType.GoogleOAuth;
Expand Down
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ export class WorkOSSSOStrategy extends Strategy {

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

Expand All @@ -60,6 +62,7 @@ export class WorkOSSSOStrategy extends Strategy {
connection,
organization,
provider,
domain: domain || email?.slice(email.indexOf("@") + 1),
clientID: this.options.clientID,
redirectURI: options.redirectURI || this.options.callbackURL,
...options,
Expand Down

0 comments on commit 9888b32

Please sign in to comment.