xuanwu-sso-sdk 1.0.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/README.md +115 -0
- package/package.json +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# @xuanwu/sso-sdk
|
|
2
|
+
|
|
3
|
+
Xuanwu SSO SDK for business systems integration.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @xuanwu/sso-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Server-side (Node.js / Next.js API routes)
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { verifyToken, getUserInfo, authenticate } from '@xuanwu/sso-sdk'
|
|
17
|
+
|
|
18
|
+
// Verify token locally (fast, no network call)
|
|
19
|
+
const payload = verifyToken(token)
|
|
20
|
+
if (!payload) {
|
|
21
|
+
return { error: 'Invalid token' }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Get full user info from SSO (makes network call)
|
|
25
|
+
const { valid, user } = await getUserInfo(token)
|
|
26
|
+
|
|
27
|
+
// Authenticate - combined local verify + remote getUserInfo
|
|
28
|
+
const { valid, user } = await authenticate(token)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Client-side (React)
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { useSSO } from '@xuanwu/sso-sdk'
|
|
35
|
+
|
|
36
|
+
function LoginButton() {
|
|
37
|
+
const { user, loading, isAuthenticated, loginWithSSO, logout } = useSSO(
|
|
38
|
+
process.env.NEXT_PUBLIC_SSO_URL || ''
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
if (loading) return <p>Loading...</p>
|
|
42
|
+
|
|
43
|
+
if (isAuthenticated) {
|
|
44
|
+
return (
|
|
45
|
+
<div>
|
|
46
|
+
<p>Welcome, {user.name}</p>
|
|
47
|
+
<button onClick={logout}>Logout</button>
|
|
48
|
+
</div>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return <button onClick={() => loginWithSSO()}>Login with SSO</button>
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Environment Variables
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# Required for client-side
|
|
60
|
+
NEXT_PUBLIC_SSO_URL=http://localhost:3000
|
|
61
|
+
|
|
62
|
+
# Required for server-side
|
|
63
|
+
SSO_URL=http://localhost:3000
|
|
64
|
+
SSO_JWT_SECRET=your-shared-jwt-secret
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## API
|
|
68
|
+
|
|
69
|
+
### `verifyToken(token: string): TokenPayload | null`
|
|
70
|
+
|
|
71
|
+
Verify JWT token locally without network call. Returns the token payload or null if invalid.
|
|
72
|
+
|
|
73
|
+
### `getUserInfo(token: string): Promise<{ valid: boolean; user: SSOUser | null }>`
|
|
74
|
+
|
|
75
|
+
Fetch full user information from SSO server.
|
|
76
|
+
|
|
77
|
+
### `authenticate(token: string): Promise<{ valid: boolean; user: SSOUser | null }>`
|
|
78
|
+
|
|
79
|
+
Combined local verification and remote user info fetch.
|
|
80
|
+
|
|
81
|
+
### `getLoginUrl(redirectUri: string, ssoUrl?: string): string`
|
|
82
|
+
|
|
83
|
+
Generate SSO login URL with redirect.
|
|
84
|
+
|
|
85
|
+
### `extractToken(request): string | null`
|
|
86
|
+
|
|
87
|
+
Extract token from request (supports Authorization header and cookies).
|
|
88
|
+
|
|
89
|
+
### `parseTokenFromCallback(url: string): string | null`
|
|
90
|
+
|
|
91
|
+
Extract token from callback URL.
|
|
92
|
+
|
|
93
|
+
### `useSSO(ssoUrl: string, autoFetch?: boolean): UseSSOResult`
|
|
94
|
+
|
|
95
|
+
React hook for authentication state management.
|
|
96
|
+
|
|
97
|
+
## Types
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
interface SSOUser {
|
|
101
|
+
id: string
|
|
102
|
+
feishuUnionId: string | null
|
|
103
|
+
email: string | null
|
|
104
|
+
name: string | null
|
|
105
|
+
avatarUrl: string | null
|
|
106
|
+
role: 'ADMIN' | 'USER'
|
|
107
|
+
createdAt: string | null
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
interface TokenPayload {
|
|
111
|
+
userId: string
|
|
112
|
+
feishuUnionId: string
|
|
113
|
+
role: string
|
|
114
|
+
}
|
|
115
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "xuanwu-sso-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Xuanwu SSO SDK for business systems",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
20
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"jsonwebtoken": "^9.0.2"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/jsonwebtoken": "^9.0.5",
|
|
27
|
+
"tsup": "^8.0.0",
|
|
28
|
+
"typescript": "^5.3.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"next": ">=14.0.0"
|
|
32
|
+
}
|
|
33
|
+
}
|