@aomex-contrib/oauth2-linkedin 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +116 -2
- package/dist/index.d.ts +40 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @aomex-contrib/oauth2-linkedin
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`013319f`](https://github.com/aomex/aomex-contrib/commit/013319f96e95278a693f55708798733412f0a947) Thanks [@geekact](https://github.com/geekact)! - 新增厂商 OAuth2 / OIDC 客户端包:Google、Microsoft、Apple、GitHub、Discord、GitLab、LinkedIn、Facebook、Twitter(X)、Spotify。
|
|
8
|
+
|
|
9
|
+
- 纯 OAuth2 厂商默认 `callback` 返回 `{ base, userinfo }`
|
|
10
|
+
- Apple:`form_post` + JWT `client_secret`;默认 profile 为 `{ claims, user }`(`user` 仅首次授权可能有姓名)
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- Updated dependencies [[`013319f`](https://github.com/aomex/aomex-contrib/commit/013319f96e95278a693f55708798733412f0a947)]:
|
|
15
|
+
- @aomex-contrib/oauth2-preset@0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-2024 geekact
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,117 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @aomex-contrib/oauth2-linkedin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
LinkedIn OAuth2(OpenID Connect 产品形态)登录预设。默认 `callback` 拉取 [`GET /v2/userinfo`](https://learn.microsoft.com/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2),返回 `{ base, userinfo }`。
|
|
4
|
+
|
|
5
|
+
官方文档:[Authorization Code Flow](https://learn.microsoft.com/linkedin/shared/authentication/authorization-code-flow)。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 1. 安装
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @aomex-contrib/oauth2-linkedin @aomex/oauth2-client
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## 2. LinkedIn 后台准备
|
|
18
|
+
|
|
19
|
+
1. 打开 [LinkedIn Developers](https://www.linkedin.com/developers/apps) → Create app
|
|
20
|
+
2. Auth 选项卡:
|
|
21
|
+
- 复制 **Client ID**、**Client Secret**
|
|
22
|
+
- Authorized redirect URLs for your app:添加
|
|
23
|
+
`https://example.com/oauth/linkedin/callback`
|
|
24
|
+
3. Products:申请 / 启用 **Sign In with LinkedIn using OpenID Connect**(名称以控制台为准)
|
|
25
|
+
4. 确认 OAuth scopes 含 `openid`、`profile`、`email`(本包默认)
|
|
26
|
+
|
|
27
|
+
| 环境变量建议 | 对应 |
|
|
28
|
+
| ------------------------ | ------------- |
|
|
29
|
+
| `LINKEDIN_CLIENT_ID` | Client ID |
|
|
30
|
+
| `LINKEDIN_CLIENT_SECRET` | Client Secret |
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## 3. store
|
|
35
|
+
|
|
36
|
+
保存 OAuth `state`。生产用共享 Redis。
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 4. 最小可跑通示例
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { CacheMemoryAdapter } from '@aomex/cache/memory';
|
|
44
|
+
import { LinkedInOAuth2Client } from '@aomex-contrib/oauth2-linkedin';
|
|
45
|
+
|
|
46
|
+
const linkedin = new LinkedInOAuth2Client({
|
|
47
|
+
clientId: process.env.LINKEDIN_CLIENT_ID!,
|
|
48
|
+
clientSecret: process.env.LINKEDIN_CLIENT_SECRET!,
|
|
49
|
+
redirectUri: 'https://example.com/oauth/linkedin/callback',
|
|
50
|
+
store: new CacheMemoryAdapter(),
|
|
51
|
+
// 默认 scopes: openid, profile, email
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
router.get('/oauth/linkedin', {
|
|
55
|
+
action: async (ctx) => {
|
|
56
|
+
await linkedin.redirect(ctx);
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
router.get('/oauth/linkedin/callback', {
|
|
61
|
+
action: async (ctx) => {
|
|
62
|
+
const profile = await linkedin.callback(ctx);
|
|
63
|
+
// profile.base = 令牌;profile.userinfo = LinkedInUser
|
|
64
|
+
ctx.send(profile);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## 5. 默认返回什么
|
|
72
|
+
|
|
73
|
+
默认 `callback` 返回:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
{
|
|
77
|
+
base: TokenSet; // access_token 等
|
|
78
|
+
userinfo: LinkedInUser; // 来自 `GET /v2/userinfo`
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`LinkedInUser` 常见字段:`sub`、`name`、`given_name`、`family_name`、`picture`、`email`、`email_verified`。
|
|
83
|
+
|
|
84
|
+
自定义 `onProfile` 时框架已拉好 `userinfo`:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
onProfile: async ({ token, userinfo }) => {
|
|
88
|
+
return upsertUser({ id: userinfo.id /* 或对应字段 */ });
|
|
89
|
+
},
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
也可直接:`import { fetchLinkedInUser, linkedin } from '@aomex-contrib/oauth2-linkedin'`。
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 6. 默认预设
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
authorizationEndpoint: 'https://www.linkedin.com/oauth/v2/authorization';
|
|
100
|
+
tokenEndpoint: 'https://www.linkedin.com/oauth/v2/accessToken';
|
|
101
|
+
revocationEndpoint: 'https://www.linkedin.com/oauth/v2/revoke';
|
|
102
|
+
scopes: ['openid', 'profile', 'email'];
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## 7. 常见问题
|
|
108
|
+
|
|
109
|
+
**Q: unauthorized_scope_error / 产品未开通?**
|
|
110
|
+
A: 在 App 的 Products 里启用 Sign In with LinkedIn (OIDC),并等待审核通过(若需要)。
|
|
111
|
+
|
|
112
|
+
**Q: redirect_uri 不匹配?**
|
|
113
|
+
A: Auth 页 Authorized redirect URLs 必须包含完整回调地址。
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
通用能力见:https://aomex.js.org
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { OAuth2Client } from "@aomex/oauth2-client";
|
|
2
|
+
import { OAuth2Profile, OAuth2Profile as OAuth2Profile$1, OAuth2VendorOnProfileArgs, OAuth2VendorOptions, OAuth2VendorOptions as OAuth2VendorOptions$1 } from "@aomex-contrib/oauth2-preset";
|
|
3
|
+
/**
|
|
4
|
+
* LinkedIn OAuth2 客户端预设
|
|
5
|
+
* @see https://learn.microsoft.com/linkedin/shared/authentication/authorization-code-flow
|
|
6
|
+
*/
|
|
7
|
+
export declare const linkedin: {
|
|
8
|
+
readonly authorizationEndpoint: 'https://www.linkedin.com/oauth/v2/authorization';
|
|
9
|
+
readonly tokenEndpoint: 'https://www.linkedin.com/oauth/v2/accessToken';
|
|
10
|
+
readonly revocationEndpoint: 'https://www.linkedin.com/oauth/v2/revoke';
|
|
11
|
+
readonly scopes: readonly ['openid', 'profile', 'email'];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* OIDC UserInfo(`GET /v2/userinfo`)常见字段
|
|
15
|
+
* @see https://learn.microsoft.com/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2
|
|
16
|
+
*/
|
|
17
|
+
export type LinkedInUser = {
|
|
18
|
+
/** 主体标识(OpenID `sub`) */
|
|
19
|
+
sub: string;
|
|
20
|
+
/** 全名 */
|
|
21
|
+
name?: string;
|
|
22
|
+
/** 名 */
|
|
23
|
+
given_name?: string;
|
|
24
|
+
/** 姓 */
|
|
25
|
+
family_name?: string;
|
|
26
|
+
/** 头像 URL */
|
|
27
|
+
picture?: string;
|
|
28
|
+
/** 邮箱;需 `email` scope */
|
|
29
|
+
email?: string;
|
|
30
|
+
/** 邮箱是否已验证 */
|
|
31
|
+
email_verified?: boolean;
|
|
32
|
+
};
|
|
33
|
+
export declare function fetchLinkedInUser(accessToken: string): Promise<LinkedInUser>;
|
|
34
|
+
/**
|
|
35
|
+
* LinkedIn OAuth2 客户端;默认 `callback` 返回 `{ base, userinfo }`(`userinfo` 来自 `/v2/userinfo`)
|
|
36
|
+
*/
|
|
37
|
+
export declare class LinkedInOAuth2Client<Profile = OAuth2Profile$1<LinkedInUser>> extends OAuth2Client<Profile> {
|
|
38
|
+
constructor(opts: OAuth2VendorOptions$1<Profile, LinkedInUser>);
|
|
39
|
+
}
|
|
40
|
+
export type { OAuth2Profile, OAuth2VendorOnProfileArgs, OAuth2VendorOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { OAuth2Client } from "@aomex/oauth2-client";
|
|
2
|
+
import { mergeOAuth2Preset } from "@aomex-contrib/oauth2-preset";
|
|
3
|
+
// src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* LinkedIn OAuth2 客户端预设
|
|
6
|
+
* @see https://learn.microsoft.com/linkedin/shared/authentication/authorization-code-flow
|
|
7
|
+
*/
|
|
8
|
+
const linkedin = {
|
|
9
|
+
authorizationEndpoint: "https://www.linkedin.com/oauth/v2/authorization",
|
|
10
|
+
tokenEndpoint: "https://www.linkedin.com/oauth/v2/accessToken",
|
|
11
|
+
revocationEndpoint: "https://www.linkedin.com/oauth/v2/revoke",
|
|
12
|
+
scopes: [
|
|
13
|
+
"openid",
|
|
14
|
+
"profile",
|
|
15
|
+
"email"
|
|
16
|
+
]
|
|
17
|
+
};
|
|
18
|
+
async function fetchLinkedInUser(accessToken) {
|
|
19
|
+
const res = await fetch("https://api.linkedin.com/v2/userinfo", { headers: { Authorization: `Bearer ${accessToken}` } });
|
|
20
|
+
if (!res.ok) throw new Error(`LinkedIn /v2/userinfo failed: ${res.status}`);
|
|
21
|
+
return await res.json();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* LinkedIn OAuth2 客户端;默认 `callback` 返回 `{ base, userinfo }`(`userinfo` 来自 `/v2/userinfo`)
|
|
25
|
+
*/
|
|
26
|
+
var LinkedInOAuth2Client = class extends OAuth2Client {
|
|
27
|
+
constructor(opts) {
|
|
28
|
+
super(mergeOAuth2Preset(linkedin, opts, fetchLinkedInUser));
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
export { LinkedInOAuth2Client, fetchLinkedInUser, linkedin };
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { OAuth2Client } from '@aomex/oauth2-client';\nimport {\n mergeOAuth2Preset,\n type OAuth2Profile,\n type OAuth2VendorOptions,\n} from '@aomex-contrib/oauth2-preset';\n\n/**\n * LinkedIn OAuth2 客户端预设\n * @see https://learn.microsoft.com/linkedin/shared/authentication/authorization-code-flow\n */\nexport const linkedin = {\n authorizationEndpoint: 'https://www.linkedin.com/oauth/v2/authorization',\n tokenEndpoint: 'https://www.linkedin.com/oauth/v2/accessToken',\n revocationEndpoint: 'https://www.linkedin.com/oauth/v2/revoke',\n scopes: ['openid', 'profile', 'email'],\n} as const;\n\n/**\n * OIDC UserInfo(`GET /v2/userinfo`)常见字段\n * @see https://learn.microsoft.com/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2\n */\nexport type LinkedInUser = {\n /** 主体标识(OpenID `sub`) */\n sub: string;\n /** 全名 */\n name?: string;\n /** 名 */\n given_name?: string;\n /** 姓 */\n family_name?: string;\n /** 头像 URL */\n picture?: string;\n /** 邮箱;需 `email` scope */\n email?: string;\n /** 邮箱是否已验证 */\n email_verified?: boolean;\n};\n\nexport async function fetchLinkedInUser(accessToken: string): Promise<LinkedInUser> {\n const res = await fetch('https://api.linkedin.com/v2/userinfo', {\n headers: { Authorization: `Bearer ${accessToken}` },\n });\n if (!res.ok) {\n throw new Error(`LinkedIn /v2/userinfo failed: ${res.status}`);\n }\n return (await res.json()) as LinkedInUser;\n}\n\n/**\n * LinkedIn OAuth2 客户端;默认 `callback` 返回 `{ base, userinfo }`(`userinfo` 来自 `/v2/userinfo`)\n */\nexport class LinkedInOAuth2Client<\n Profile = OAuth2Profile<LinkedInUser>,\n> extends OAuth2Client<Profile> {\n constructor(opts: OAuth2VendorOptions<Profile, LinkedInUser>) {\n super(mergeOAuth2Preset(linkedin, opts, fetchLinkedInUser));\n }\n}\n\nexport type {\n OAuth2Profile,\n OAuth2VendorOptions,\n OAuth2VendorOnProfileArgs,\n} from '@aomex-contrib/oauth2-preset';\n"],"mappings":";;;;;;;AAWA,MAAa,WAAW;CACtB,uBAAuB;CACvB,eAAe;CACf,oBAAoB;CACpB,QAAQ;EAAC;EAAU;EAAW;CAAO;AACvC;AAuBA,eAAsB,kBAAkB,aAA4C;CAClF,MAAM,MAAM,MAAM,MAAM,wCAAwC,EAC9D,SAAS,EAAE,eAAe,UAAU,cAAc,EACpD,CAAC;CACD,IAAI,CAAC,IAAI,IACP,MAAM,IAAI,MAAM,iCAAiC,IAAI,QAAQ;CAE/D,OAAQ,MAAM,IAAI,KAAK;AACzB;;;;AAKA,IAAa,uBAAb,cAEU,aAAsB;CAC9B,YAAY,MAAkD;EAC5D,MAAM,kBAAkB,UAAU,MAAM,iBAAiB,CAAC;CAC5D;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aomex-contrib/oauth2-linkedin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "LinkedIn OAuth2 客户端预设",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git@github.com:aomex/aomex-contrib.git"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://aomex.js.org",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"package.json",
|
|
25
|
+
"README.md",
|
|
26
|
+
"CHANGELOG.md"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@aomex/oauth2-client": "^4.3.0"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@aomex-contrib/oauth2-preset": "^0.1.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@aomex/cache": "^4.3.0",
|
|
39
|
+
"@aomex/common": "^4.3.0",
|
|
40
|
+
"@aomex/oauth2-client": "^4.3.0",
|
|
41
|
+
"@aomex/web": "^4.3.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {}
|
|
6
44
|
}
|