Comparing version 0.2.4 to 0.2.5
@@ -11,7 +11,11 @@ import { Context } from 'egg'; | ||
private moziRedirectUrl; | ||
private moziBindRedirectUrl; | ||
private googleRedirectUrl; | ||
private googleBindRedirectUrl; | ||
private miniprogramRedirectUrl; | ||
private miniprogramBindRedirectUrl; | ||
private loginRedirectUri; | ||
private unautorizedRedirectUri?; | ||
constructor(kauthConfig: KauthConfig); | ||
dispatch: (ctx: Context, next: () => Promise<void>) => Promise<void>; | ||
dispatch: (ctx: Context, next: (params?: any) => Promise<void>) => Promise<void>; | ||
redirectMoziAuthorizeUri: (ctx: Context) => Promise<void>; | ||
@@ -23,6 +27,13 @@ redirectGoogleAuthorizeUri: (ctx: Context) => Promise<void>; | ||
redirectMiniprogramToken: (ctx: Context) => Promise<void>; | ||
bindMozi: (ctx: Context) => Promise<void>; | ||
bindGoogle: (ctx: Context) => Promise<void>; | ||
bindMiniprogram: (ctx: Context) => Promise<void>; | ||
fallback: (ctx: Context, next: () => Promise<void>) => Promise<void>; | ||
illegalToBindAccount(ctx: Context): void; | ||
private getBindingUserIdFromCookie; | ||
private handleAuthenticationInformation; | ||
private saveAuthenticationInformationToContext; | ||
private saveJsonWebTokenToHeader; | ||
private formatState; | ||
private parseState; | ||
} |
@@ -5,22 +5,2 @@ "use strict"; | ||
const kauth_sdk_node_1 = require("kauth-sdk-node"); | ||
class KauthRouter { | ||
constructor() { | ||
this._routes = {}; | ||
} | ||
route(path, callback) { | ||
this._routes[path] = callback; | ||
} | ||
fallback(callback) { | ||
this._fallback = callback; | ||
} | ||
async dispatch(ctx, next) { | ||
const callback = this._routes[ctx.request.path]; | ||
if (callback) { | ||
await callback(ctx, next); | ||
} | ||
else { | ||
await this._fallback(ctx, next); | ||
} | ||
} | ||
} | ||
class KauthProvider { | ||
@@ -33,11 +13,51 @@ constructor(kauthConfig) { | ||
this.redirectMoziAuthorizeUri = async (ctx) => { | ||
const url = await this.moziAuthenticator.authorizeURL(this.moziRedirectUrl); | ||
const { type } = ctx.query; | ||
let url = ''; | ||
if (type === 'binding') { | ||
const userId = await this.getBindingUserIdFromCookie(ctx); | ||
if (!userId) { | ||
return this.illegalToBindAccount(ctx); | ||
} | ||
const state = this.formatState(userId ? { userId } : {}); | ||
url = await this.moziAuthenticator.authorizeURL(this.moziBindRedirectUrl, state); | ||
} | ||
else { | ||
const state = this.formatState({}); | ||
url = await this.moziAuthenticator.authorizeURL(this.moziRedirectUrl, state); | ||
} | ||
ctx.redirect(url); | ||
}; | ||
this.redirectGoogleAuthorizeUri = async (ctx) => { | ||
const url = await this.googleAuthenticator.authorizeURL(this.googleRedirectUrl); | ||
const { type } = ctx.query; | ||
let url = ''; | ||
if (type === 'binding') { | ||
const userId = await this.getBindingUserIdFromCookie(ctx); | ||
console.log(userId); | ||
if (!userId) { | ||
return this.illegalToBindAccount(ctx); | ||
} | ||
const state = this.formatState({ userId }); | ||
url = await this.googleAuthenticator.authorizeURL(this.googleBindRedirectUrl, state); | ||
} | ||
else { | ||
const state = this.formatState({}); | ||
url = await this.googleAuthenticator.authorizeURL(this.googleRedirectUrl, state); | ||
} | ||
ctx.redirect(url); | ||
}; | ||
this.redirectMiniProgarmAuthorizeUri = async (ctx) => { | ||
const url = await this.miniprogramAuthenticator.authorizeURL(this.miniprogramRedirectUrl); | ||
const { type } = ctx.query; | ||
let url = ''; | ||
if (type === 'binding') { | ||
const userId = await this.getBindingUserIdFromCookie(ctx); | ||
if (!userId) { | ||
return this.illegalToBindAccount(ctx); | ||
} | ||
const state = this.formatState({ userId }); | ||
url = await this.miniprogramAuthenticator.authorizeURL(this.miniprogramBindRedirectUrl, state); | ||
} | ||
else { | ||
const state = this.formatState({}); | ||
url = await this.miniprogramAuthenticator.authorizeURL(this.miniprogramRedirectUrl, state); | ||
} | ||
ctx.redirect(url); | ||
@@ -57,2 +77,20 @@ }; | ||
}; | ||
this.bindMozi = async (ctx) => { | ||
const { code, state } = ctx.query; | ||
const { userId } = this.parseState(state); | ||
await this.moziAuthenticator.bind(userId, code, this.moziBindRedirectUrl); | ||
ctx.redirect(this.loginRedirectUri); | ||
}; | ||
this.bindGoogle = async (ctx) => { | ||
const { code, state } = ctx.query; | ||
const { userId } = this.parseState(state); | ||
await this.googleAuthenticator.bind(userId, code, this.googleBindRedirectUrl); | ||
ctx.redirect(this.loginRedirectUri); | ||
}; | ||
this.bindMiniprogram = async (ctx) => { | ||
const { code, state } = ctx.query; | ||
const { userId } = this.parseState(state); | ||
await this.miniprogramAuthenticator.bind(userId, code, this.miniprogramBindRedirectUrl); | ||
ctx.redirect(this.loginRedirectUri); | ||
}; | ||
this.fallback = async (ctx, next) => { | ||
@@ -63,2 +101,5 @@ // verify | ||
if (!authenticationInformation) { | ||
if (this.unautorizedRedirectUri) { | ||
return ctx.redirect(this.unautorizedRedirectUri); | ||
} | ||
ctx.status = 401; | ||
@@ -86,2 +127,8 @@ return; | ||
}; | ||
this.formatState = (obj = {}) => { | ||
return Buffer.from(JSON.stringify(obj)).toString('base64'); | ||
}; | ||
this.parseState = (str = '') => { | ||
return JSON.parse(Buffer.from(str, 'base64').toString('utf8')); | ||
}; | ||
const kauthApi = new kauth_sdk_node_1.KauthApi(kauthConfig); | ||
@@ -93,5 +140,9 @@ this.moziAuthenticator = new kauth_sdk_node_1.MoziAuthenticator(kauthApi); | ||
this.moziRedirectUrl = kauthConfig.origin + kauthConfig.moziTokenUri; | ||
this.moziBindRedirectUrl = kauthConfig.origin + kauthConfig.moziBindUri; | ||
this.googleRedirectUrl = kauthConfig.origin + kauthConfig.googleTokenUri; | ||
this.googleBindRedirectUrl = kauthConfig.origin + kauthConfig.googleBindUri; | ||
this.miniprogramRedirectUrl = kauthConfig.origin + kauthConfig.miniprogramTokenUri; | ||
this.miniprogramBindRedirectUrl = kauthConfig.origin + kauthConfig.miniprogramBindUri; | ||
this.loginRedirectUri = kauthConfig.loginRedirectUri; | ||
this.unautorizedRedirectUri = kauthConfig.unautorizedRedirectUri; | ||
const router = new KauthRouter(); | ||
@@ -104,6 +155,77 @@ router.route(kauthConfig.moziAuthorizeUri, this.redirectMoziAuthorizeUri); | ||
router.route(kauthConfig.miniprogramTokenUri, this.redirectMiniprogramToken); | ||
router.route(kauthConfig.moziBindUri, this.bindMozi); | ||
router.route(kauthConfig.googleBindUri, this.bindGoogle); | ||
router.route(kauthConfig.miniprogramBindUri, this.bindMiniprogram); | ||
router.fallback(this.fallback); | ||
this.router = router; | ||
} | ||
illegalToBindAccount(ctx) { | ||
ctx.status = 500; | ||
ctx.body = { | ||
message: 'illegal to bind account', | ||
}; | ||
} | ||
async getBindingUserIdFromCookie(ctx) { | ||
const token = ctx.cookies.get('Authorization'); | ||
const authenticationInformation = await this.jwtProvider.verify(token); | ||
return authenticationInformation && authenticationInformation.primaryPrincipal.id; | ||
} | ||
} | ||
exports.KauthProvider = KauthProvider; | ||
class KauthRouter { | ||
constructor() { | ||
this._routes = {}; | ||
this._routeComputers = []; | ||
} | ||
route(path, callback) { | ||
this._routes[path] = callback; | ||
} | ||
compute(path, callback) { | ||
let str = ''; | ||
const paramKeys = []; | ||
path.split('/').forEach(subPath => { | ||
if (subPath) { | ||
if (subPath.startsWith(':')) { | ||
str += '/([^/]*)'; | ||
paramKeys.push(subPath.substr(1)); | ||
} | ||
else { | ||
str += '/' + subPath; | ||
} | ||
} | ||
}); | ||
const regExp = new RegExp(str); | ||
this._routeComputers.push({ | ||
match: (currentPath) => { | ||
const paramValues = regExp.exec(currentPath); | ||
if (!paramValues) { | ||
return null; | ||
} | ||
const result = {}; | ||
for (let i = 0; i < paramKeys.length; i++) { | ||
result[paramKeys[i]] = paramValues[i + 1]; | ||
} | ||
return result; | ||
}, | ||
callback, | ||
}); | ||
} | ||
fallback(callback) { | ||
this._fallback = callback; | ||
} | ||
async dispatch(ctx, next) { | ||
const callback = this._routes[ctx.request.path]; | ||
if (callback) { | ||
await callback(ctx, next); | ||
return; | ||
} | ||
for (const computer of this._routeComputers) { | ||
const params = computer.match(ctx.request.path); | ||
if (params) { | ||
await computer.callback(ctx, next, params); | ||
return; | ||
} | ||
} | ||
await this._fallback(ctx, next); | ||
} | ||
} |
@@ -0,1 +1,16 @@ | ||
## [0.2.5](https://gitlab-ag.marmot-cloud.com/marmot/kauth-sdk-egg/compare/v0.2.4...v0.2.5) (2020-09-27) | ||
### Bug Fixes | ||
* remove additional config ([4e2d636](https://gitlab-ag.marmot-cloud.com/marmot/kauth-sdk-egg/commit/4e2d63677392d79b068f290f46d20373106ce82d)) | ||
### Features | ||
* update kauth-sdk-node version ([79eeb2a](https://gitlab-ag.marmot-cloud.com/marmot/kauth-sdk-egg/commit/79eeb2a814761ce401a866acebb5a192ab7d2676)) | ||
* update kauth-sdk-node version ([c4a4111](https://gitlab-ag.marmot-cloud.com/marmot/kauth-sdk-egg/commit/c4a41118fc4d00e2ede009fd3604407f79d8b4a7)) | ||
## [0.2.4](https://gitlab-ag.marmot-cloud.com/marmot/kauth-sdk-egg/compare/v0.2.3...v0.2.4) (2020-09-07) | ||
@@ -2,0 +17,0 @@ |
export declare const kauth: { | ||
jwtSecret: string; | ||
jwtDuration: string; | ||
jwtDuration: number; | ||
googleAuthorizeUri: string; | ||
googleTokenUri: string; | ||
googleBindUri: string; | ||
moziAuthorizeUri: string; | ||
moziTokenUri: string; | ||
moziBindUri: string; | ||
miniprogramAuthorizeUri: string; | ||
miniprogramTokenUri: string; | ||
miniprogramBindUri: string; | ||
loginRedirectUri: string; | ||
@@ -11,0 +14,0 @@ kauthOrigin: string; |
@@ -6,9 +6,12 @@ "use strict"; | ||
jwtSecret: 'secret', | ||
jwtDuration: '3600', | ||
jwtDuration: 3600, | ||
googleAuthorizeUri: '/api/kauth/google/authorize', | ||
googleTokenUri: '/api/kauth/google/token', | ||
googleBindUri: '/api/kauth/google/bind', | ||
moziAuthorizeUri: '/api/kauth/mozi/authorize', | ||
moziTokenUri: '/api/kauth/mozi/token', | ||
moziBindUri: '/api/kauth/mozi/bind', | ||
miniprogramAuthorizeUri: '/api/kauth/miniprogram/authorize', | ||
miniprogramTokenUri: '/api/kauth/miniprogram/token', | ||
miniprogramBindUri: '/api/kauth/miniprogram/bind', | ||
loginRedirectUri: '/', | ||
@@ -15,0 +18,0 @@ kauthOrigin: 'http://global.oapi.marmot-cloud.com', |
export declare const kauth: { | ||
jwtSecret: string; | ||
jwtDuration: string; | ||
googleAuthorizeUri: string; | ||
googleTokenUri: string; | ||
moziAuthorizeUri: string; | ||
moziTokenUri: string; | ||
miniprogramAuthorizeUri: string; | ||
miniprogramTokenUri: string; | ||
loginRedirectUri: string; | ||
kauthOrigin: string; | ||
kauthServiceName: string; | ||
kauthServicePort: number; | ||
kflowOrigin: string; | ||
kflowServiceName: string; | ||
kflowServicePort: number; | ||
}; |
@@ -5,17 +5,4 @@ "use strict"; | ||
exports.kauth = { | ||
jwtSecret: 'secret', | ||
jwtDuration: '3600', | ||
googleAuthorizeUri: '/api/kauth/google/authorize', | ||
googleTokenUri: '/api/kauth/google/token', | ||
moziAuthorizeUri: '/api/kauth/mozi/authorize', | ||
moziTokenUri: '/api/kauth/mozi/token', | ||
miniprogramAuthorizeUri: '/api/kauth/miniprogram/authorize', | ||
miniprogramTokenUri: '/api/kauth/miniprogram/token', | ||
loginRedirectUri: '/', | ||
kauthOrigin: 'http://global.oapi.marmot-cloud.com', | ||
kauthServiceName: 'kauth-svc.prod.svc.cluster.local', | ||
kauthServicePort: 80, | ||
kflowOrigin: 'http://global.oapi.marmot-cloud.com', | ||
kflowServiceName: 'kflow-svc.prod.svc.cluster.local', | ||
kflowServicePort: 80 | ||
}; |
{ | ||
"name": "egg-kauth", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "egg kauth plugin", | ||
@@ -31,3 +31,3 @@ "eggPlugin": { | ||
"dependencies": { | ||
"kauth-sdk-node": "^0.1.0" | ||
"kauth-sdk-node": "^0.1.1" | ||
}, | ||
@@ -34,0 +34,0 @@ "devDependencies": { |
import { AuthenticationInformation } from "kauth-sdk-node"; | ||
declare module 'egg' { | ||
interface Application { | ||
kauth: { | ||
isPermit: (value: string) => (ctx: any, next: any) => Promise<void>; | ||
}; | ||
} | ||
interface Context { | ||
@@ -5,0 +11,0 @@ authenticationInformation: AuthenticationInformation; |
22694
15
447
Updatedkauth-sdk-node@^0.1.1