Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: createLinkedInOAuthConfig() #287

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ The following providers have pre-defined OAuth configurations:
1. [GitHub](https://deno.land/x/deno_kv_oauth/mod.ts?s=createGitHubOAuthConfig)
1. [GitLab](https://deno.land/x/deno_kv_oauth/mod.ts?s=createGitLabOAuthConfig)
1. [Google](https://deno.land/x/deno_kv_oauth/mod.ts?s=createGoogleOAuthConfig)
1. [LinkedIn](https://deno.land/x/deno_kv_oauth/mod.ts?s=createLinkedInOAuthConfig)
1. [Notion](https://deno.land/x/deno_kv_oauth/mod.ts?s=createNotionOAuthConfig)
1. [Okta](https://deno.land/x/deno_kv_oauth/mod.ts?s=createOktaOAuthConfig)
1. [Patreon](https://deno.land/x/deno_kv_oauth/mod.ts?s=createPatreonOAuthConfig)
Expand Down
49 changes: 49 additions & 0 deletions lib/create_linkedin_oauth_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import type { OAuth2ClientConfig } from "../deps.ts";
import { getRequiredEnv } from "./get_required_env.ts";

/**
* Returns the OAuth configuration for LinkedIn.
*
* Requires `--allow-env[=LINKEDIN_CLIENT_ID,LINKEDIN_CLIENT_SECRET]` permissions
* and environment variables:
* 1. `LINKEDIN_CLIENT_ID`
* 2. `LINKEDIN_CLIENT_SECRET`
*
* @example
* ```ts
* import { createLinkedInOAuthConfig } from "https://deno.land/x/deno_kv_oauth@$VERSION/mod.ts";
*
* const oauthConfig = createLinkedInOAuthConfig({
* redirectUri: "http://localhost:8000/callback",
* scope: "r_liteprofile r_emailaddress"
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
* });
* ```
*
* @see {@link https://learn.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin%2Fcontext&tabs=HTTPS1}
*/
export function createLinkedInOAuthConfig(config: {
/** @see {@linkcode OAuth2ClientConfig.redirectUri} */
redirectUri: string;
/** @see {@linkcode OAuth2ClientConfig.defaults}*/
scope: string | string[];
}): OAuth2ClientConfig {
const clientId = getRequiredEnv("LINKEDIN_CLIENT_ID");
const clientSecret = getRequiredEnv("LINKEDIN_CLIENT_SECRET");
return {
clientId,
clientSecret,
authorizationEndpointUri: "https://www.linkedin.com/oauth/v2/authorization",
tokenUri: "https://www.linkedin.com/oauth/v2/accessToken",
redirectUri: config.redirectUri,
defaults: {
requestOptions: {
body: {
client_id: clientId,
client_secret: clientSecret,
},
},
scope: config.scope,
},
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't get access toke with this settings. https://www.linkedin.com/oauth/v2/accessToken seems returning invalid_client error.

Error: Client authentication failed
    at AuthorizationCodeGrant.getTokenResponseError (https://deno.land/x/oauth2_client@v1.0.2/src/grant_base.ts:156:14)
    at eventLoopTick (ext:core/01_core.js:182:7)
    at async AuthorizationCodeGrant.parseTokenResponse (https://deno.land/x/oauth2_client@v1.0.2/src/grant_base.ts:72:13)
    at async handleCallback (file:///Users/kt3k/denoland/deno_kv_oauth/lib/handle_callback.ts:69:18)
    at async handler (file:///Users/kt3k/denoland/deno_kv_oauth/server.ts:22:28)
    at async ext:deno_http/00_serve.js:460:18 {
  error: "invalid_client",
  errorDescription: "Client authentication failed",
  errorUri: undefined,
  state: undefined
}

}
5 changes: 5 additions & 0 deletions lib/create_oauth_config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createFacebookOAuthConfig } from "./create_facebook_oauth_config.ts";
import { createGitHubOAuthConfig } from "./create_github_oauth_config.ts";
import { createGitLabOAuthConfig } from "./create_gitlab_oauth_config.ts";
import { createGoogleOAuthConfig } from "./create_google_oauth_config.ts";
import { createLinkedInOAuthConfig } from "./create_linkedin_oauth_config.ts";
import { createNotionOAuthConfig } from "./create_notion_oauth_config.ts";
import { createOktaOAuthConfig } from "./create_okta_oauth_config.ts";
import { createPatreonOAuthConfig } from "./create_patreon_oauth_config.ts";
Expand Down Expand Up @@ -43,6 +44,10 @@ import { createTwitterOAuthConfig } from "./create_twitter_oauth_config.ts";
envPrefix: "GOOGLE",
createOAuthConfigFn: createGoogleOAuthConfig,
},
{
envPrefix: "LINKEDIN",
createOAuthConfigFn: createLinkedInOAuthConfig,
},
{
envPrefix: "NOTION",
createOAuthConfigFn: createNotionOAuthConfig,
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from "./lib/create_facebook_oauth_config.ts";
export * from "./lib/create_github_oauth_config.ts";
export * from "./lib/create_gitlab_oauth_config.ts";
export * from "./lib/create_google_oauth_config.ts";
export * from "./lib/create_linkedin_oauth_config.ts";
export * from "./lib/create_notion_oauth_config.ts";
export * from "./lib/create_okta_oauth_config.ts";
export * from "./lib/create_patreon_oauth_config.ts";
Expand Down
Loading