Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
feat(plugins/auth): add netzolabs provider (via google oauth)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelrk committed Jul 1, 2024
1 parent a5d2f13 commit 5f44325
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/plugins/auth/islands/auth-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ export function AuthForm(props: AuthFormProps) {
</ButtonOAuth2>
)} */
}

{!!providers?.netzolabs && (
<ButtonNetzo text={`${i18n.authForm.text} NetzoLabs`} href="/auth/netzolabs/signin">
<div className="mr-4 w-22px h-22px i-netzo-symbol" />
</ButtonNetzo>
)}
</div>
</>
);
Expand Down
5 changes: 5 additions & 0 deletions lib/plugins/auth/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export type AuthConfig = {
clientSecret?: string;
oktaDomain?: string; // must set OKTA_DOMAIN environment variable
};
netzolabs?: AuthConfigProvider & {
clientId?: string;
clientSecret?: string;
};
};
/** A function to check if a user is authorized to sign in. The function should
* throw an Error with an optional error message if not authorized.
Expand Down Expand Up @@ -132,6 +136,7 @@ export const auth = (config: AuthConfig): Plugin<NetzoState> => {
"gitlab",
"auth0",
"okta",
"netzolabs",
].some((key) => !!config?.providers?.[key as AuthProvider]);
if (!authEnabled) return { name: "netzo.auth" }; // skip if auth but no providers are set

Expand Down
23 changes: 22 additions & 1 deletion lib/plugins/auth/utils/providers/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getUserGithub } from "./github.ts";
import { getUserGitlab } from "./gitlab.ts";
import { getUserGoogle } from "./google.ts";
import { getUserNetzo, handleCallbackNetzo, signInNetzo } from "./netzo.ts";
import { getUserNetzolabs } from "./netzolabs.ts";
import { getUserOkta } from "./okta.ts";
import { getUserSlack } from "./slack.ts";

Expand All @@ -36,7 +37,10 @@ export const getAuthConfig = (provider: AuthProvider, ctx: FreshContext) => {
case "google": {
return createGoogleOAuthConfig({
redirectUri,
scope: "https://www.googleapis.com/auth/userinfo.profile",
scope: [
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email",
],
});
}
case "github": {
Expand Down Expand Up @@ -70,6 +74,21 @@ export const getAuthConfig = (provider: AuthProvider, ctx: FreshContext) => {
// case "discord":
// case "dropbox":
// case "facebook":
case "netzolabs": {
return {
clientId: Deno.env.get("NETZOLABS_CLIENT_ID")!,
clientSecret: Deno.env.get("NETZOLABS_CLIENT_SECRET")!,
authorizationEndpointUri: "https://accounts.google.com/o/oauth2/v2/auth",
tokenUri: "https://oauth2.googleapis.com/token",
redirectUri,
defaults: {
scope: [
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email",
],
},
}; // MUST be set if using Netzo Auth Provider
}
default:
throw new Error(`Provider ${provider} not supported`);
}
Expand Down Expand Up @@ -107,6 +126,8 @@ export const getUserByProvider = async (
return await getUserAuth0(accessToken);
case "okta":
return await getUserOkta(accessToken);
case "netzolabs":
return await getUserNetzolabs(accessToken);
default:
throw new Error(`Provider ${provider} not supported`);
}
Expand Down
27 changes: 27 additions & 0 deletions lib/plugins/auth/utils/providers/netzolabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { AuthUserFromProvider } from "../types.ts";
import { UserGoogle } from "./google.ts";

export type UserNetzolabs = UserGoogle;

export async function getUserNetzolabs(
accessToken: string,
): Promise<AuthUserFromProvider> {
const response = await fetch(
"https://www.googleapis.com/oauth2/v3/userinfo",
{
headers: { authorization: `Bearer ${accessToken}` },
},
);
if (!response.ok) {
const { message } = await response.json();
throw new Error(`${response.status}: ${message}`);
}
const userNetzolabs: UserNetzolabs = await response.json();
return {
provider: "netzolabs",
authId: userNetzolabs.sub,
name: userNetzolabs.name,
email: userNetzolabs.email,
avatar: userNetzolabs.picture,
};
}

0 comments on commit 5f44325

Please sign in to comment.