kauth-sdk-node
Advanced tools
Comparing version 0.0.6 to 0.0.7
import { AuthenticationInformation } from './base'; | ||
import { KauthApi } from 'src/common/http/kauth'; | ||
import { KauthApi } from '../common/http/kauth'; | ||
interface Authenticator { | ||
@@ -4,0 +4,0 @@ authenticate(token: string, redirectUri: string): Promise<AuthenticationInformation>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MoziAuthenticator = exports.GoogleAuthenticator = void 0; | ||
require("cluster"); | ||
var _mozi = require("./mozi"); | ||
var _google = require("./google"); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
const mozi_1 = require("./mozi"); | ||
const google_1 = require("./google"); | ||
class GoogleAuthenticator { | ||
constructor(kauthApi) { | ||
_defineProperty(this, "kauthApi", void 0); | ||
this.kauthApi = kauthApi; | ||
} | ||
async authenticate(token, redirectUri) { | ||
const request = { | ||
code: token, | ||
redirect_uri: redirectUri | ||
}; | ||
const result = await this.kauthApi.googleAuthenticate(request); | ||
const authenticationInformation = new _google.GoogleAuthenticationInformation(); | ||
authenticationInformation.primaryPrincipal = result.primaryPrincipal; | ||
authenticationInformation.principal = result.principal; | ||
authenticationInformation.credential = result.credential; | ||
return authenticationInformation; | ||
} | ||
authorizeURL(callback) { | ||
return this.kauthApi.googleAuthorizeURL(callback); | ||
} | ||
constructor(kauthApi) { | ||
this.kauthApi = kauthApi; | ||
} | ||
async authenticate(token, redirectUri) { | ||
const request = { | ||
code: token, | ||
redirect_uri: redirectUri, | ||
}; | ||
const result = await this.kauthApi.googleAuthenticate(request); | ||
const authenticationInformation = new google_1.GoogleAuthenticationInformation(); | ||
authenticationInformation.primaryPrincipal = result.primaryPrincipal; | ||
authenticationInformation.principal = result.principal; | ||
authenticationInformation.credential = result.credential; | ||
return authenticationInformation; | ||
} | ||
authorizeURL(callback) { | ||
return this.kauthApi.googleAuthorizeURL(callback); | ||
} | ||
} | ||
exports.GoogleAuthenticator = GoogleAuthenticator; | ||
class MoziAuthenticator { | ||
constructor(kauthApi) { | ||
_defineProperty(this, "kauthApi", void 0); | ||
this.kauthApi = kauthApi; | ||
} | ||
async authenticate(token, redirectUri) { | ||
const request = { | ||
code: token, | ||
redirect_uri: redirectUri | ||
}; | ||
const result = await this.kauthApi.moziAuthenticate(request); | ||
const authenticationInformation = new _mozi.MoziAuthenticationInformation(); | ||
authenticationInformation.primaryPrincipal = result.primaryPrincipal; | ||
authenticationInformation.principal = result.principal; | ||
authenticationInformation.credential = result.credential; | ||
return authenticationInformation; | ||
} | ||
authorizeURL(callback) { | ||
return this.kauthApi.moziAuthorizeURL(callback); | ||
} | ||
constructor(kauthApi) { | ||
this.kauthApi = kauthApi; | ||
} | ||
async authenticate(token, redirectUri) { | ||
const request = { | ||
code: token, | ||
redirect_uri: redirectUri, | ||
}; | ||
const result = await this.kauthApi.moziAuthenticate(request); | ||
const authenticationInformation = new mozi_1.MoziAuthenticationInformation(); | ||
authenticationInformation.primaryPrincipal = result.primaryPrincipal; | ||
authenticationInformation.principal = result.principal; | ||
authenticationInformation.credential = result.credential; | ||
return authenticationInformation; | ||
} | ||
authorizeURL(callback) { | ||
return this.kauthApi.moziAuthorizeURL(callback); | ||
} | ||
} | ||
exports.MoziAuthenticator = MoziAuthenticator; | ||
exports.MoziAuthenticator = MoziAuthenticator; |
@@ -6,3 +6,3 @@ export interface Principal { | ||
export interface AuthenticationInformation { | ||
primaryPrincipal: Principal; | ||
primaryPrincipal: PrimaryPrincipal; | ||
principal: Principal; | ||
@@ -15,3 +15,4 @@ credential: Credential; | ||
email: string; | ||
avatar: string; | ||
status: number; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PrimaryPrincipal = void 0; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
class PrimaryPrincipal { | ||
constructor() { | ||
_defineProperty(this, "id", void 0); | ||
_defineProperty(this, "name", void 0); | ||
_defineProperty(this, "email", void 0); | ||
_defineProperty(this, "status", void 0); | ||
} | ||
} | ||
exports.PrimaryPrincipal = PrimaryPrincipal; | ||
exports.PrimaryPrincipal = PrimaryPrincipal; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GoogleAuthenticationInformation = exports.GoogleCredential = exports.GooglePrincipal = void 0; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
class GooglePrincipal { | ||
constructor() { | ||
_defineProperty(this, "sub", void 0); | ||
_defineProperty(this, "name", void 0); | ||
_defineProperty(this, "givenName", void 0); | ||
_defineProperty(this, "familyName", void 0); | ||
_defineProperty(this, "picture", void 0); | ||
_defineProperty(this, "email", void 0); | ||
_defineProperty(this, "emailVerified", void 0); | ||
_defineProperty(this, "locale", void 0); | ||
} | ||
} | ||
exports.GooglePrincipal = GooglePrincipal; | ||
class GoogleCredential { | ||
constructor() { | ||
_defineProperty(this, "accessToken", void 0); | ||
_defineProperty(this, "expiresIn", void 0); | ||
_defineProperty(this, "refreshToken", void 0); | ||
} | ||
} | ||
exports.GoogleCredential = GoogleCredential; | ||
class GoogleAuthenticationInformation { | ||
constructor() { | ||
_defineProperty(this, "primaryPrincipal", void 0); | ||
_defineProperty(this, "principal", void 0); | ||
_defineProperty(this, "credential", void 0); | ||
} | ||
} | ||
exports.GoogleAuthenticationInformation = GoogleAuthenticationInformation; | ||
exports.GoogleAuthenticationInformation = GoogleAuthenticationInformation; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "GoogleAuthenticator", { | ||
enumerable: true, | ||
get: function () { | ||
return _authenticator.GoogleAuthenticator; | ||
} | ||
}); | ||
Object.defineProperty(exports, "MoziAuthenticator", { | ||
enumerable: true, | ||
get: function () { | ||
return _authenticator.MoziAuthenticator; | ||
} | ||
}); | ||
Object.defineProperty(exports, "AuthenticationInformation", { | ||
enumerable: true, | ||
get: function () { | ||
return _base.AuthenticationInformation; | ||
} | ||
}); | ||
Object.defineProperty(exports, "JsonWebTokenAuthenticationProvider", { | ||
enumerable: true, | ||
get: function () { | ||
return _provider.JsonWebTokenAuthenticationProvider; | ||
} | ||
}); | ||
var _authenticator = require("./authenticator"); | ||
var _base = require("./base"); | ||
var _provider = require("./provider"); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var authenticator_1 = require("./authenticator"); | ||
Object.defineProperty(exports, "GoogleAuthenticator", { enumerable: true, get: function () { return authenticator_1.GoogleAuthenticator; } }); | ||
Object.defineProperty(exports, "MoziAuthenticator", { enumerable: true, get: function () { return authenticator_1.MoziAuthenticator; } }); | ||
var provider_1 = require("./provider"); | ||
Object.defineProperty(exports, "JsonWebTokenAuthenticationProvider", { enumerable: true, get: function () { return provider_1.JsonWebTokenAuthenticationProvider; } }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MoziAuthenticationInfoManager = exports.GoogleAuthenticationInfoManger = exports.DelegatedAuthenticationInfoManager = void 0; | ||
var _base = require("./base"); | ||
var _google = require("./google"); | ||
var _mozi = require("./mozi"); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
const base_1 = require("./base"); | ||
const google_1 = require("./google"); | ||
const mozi_1 = require("./mozi"); | ||
const Sub = "sub"; | ||
const Provider = "provider"; | ||
class DelegatedAuthenticationInfoManager { | ||
constructor(kauthApi) { | ||
_defineProperty(this, "googleAuthenticationInfoManager", void 0); | ||
_defineProperty(this, "moziAuthenticationInfoManager", void 0); | ||
this.googleAuthenticationInfoManager = new GoogleAuthenticationInfoManger(kauthApi); | ||
this.moziAuthenticationInfoManager = new MoziAuthenticationInfoManager(kauthApi); | ||
} | ||
append(information, claims) { | ||
if (this.moziAuthenticationInfoManager.append(information, claims)) { | ||
return true; | ||
constructor(kauthApi) { | ||
this.googleAuthenticationInfoManager = new GoogleAuthenticationInfoManger(kauthApi); | ||
this.moziAuthenticationInfoManager = new MoziAuthenticationInfoManager(kauthApi); | ||
} | ||
if (this.googleAuthenticationInfoManager.append(information, claims)) { | ||
return true; | ||
append(information, claims) { | ||
if (this.moziAuthenticationInfoManager.append(information, claims)) { | ||
return true; | ||
} | ||
if (this.googleAuthenticationInfoManager.append(information, claims)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
return false; | ||
} | ||
transform(claims) { | ||
const provider = claims[Provider]; | ||
switch (provider) { | ||
case 'mozi': | ||
return this.moziAuthenticationInfoManager.transform(claims); | ||
case 'google': | ||
return this.googleAuthenticationInfoManager.transform(claims); | ||
transform(claims) { | ||
const provider = claims[Provider]; | ||
switch (provider) { | ||
case 'mozi': | ||
return this.moziAuthenticationInfoManager.transform(claims); | ||
case 'google': | ||
return this.googleAuthenticationInfoManager.transform(claims); | ||
} | ||
throw new Error("unkown provider for jwt " + claims[Sub]); | ||
} | ||
throw new Error("unkown provider for jwt " + claims[Sub]); | ||
} | ||
} | ||
exports.DelegatedAuthenticationInfoManager = DelegatedAuthenticationInfoManager; | ||
class GoogleAuthenticationInfoManger { | ||
constructor(kauthApi) { | ||
_defineProperty(this, "kauthApi", void 0); | ||
this.kauthApi = kauthApi; | ||
} | ||
append(information, claims) { | ||
if (information instanceof _google.GoogleAuthenticationInformation) { | ||
const specifiedInformation = information; | ||
const id = specifiedInformation.primaryPrincipal.id; | ||
claims[Sub] = String(id); | ||
claims[Provider] = 'google'; | ||
return true; | ||
constructor(kauthApi) { | ||
this.kauthApi = kauthApi; | ||
} | ||
return false; | ||
} | ||
async transform(claims) { | ||
const sub = parseInt(claims[Sub], 10); | ||
const googleAuthenticationInformation = await this.kauthApi.getGoogleAuthenticationInformation(sub); | ||
const rawPrimaryPrincipal = googleAuthenticationInformation.primaryPrincipal; | ||
const primaryPrincipal = new _base.PrimaryPrincipal(); | ||
primaryPrincipal.id = rawPrimaryPrincipal.id; | ||
primaryPrincipal.name = rawPrimaryPrincipal.name; | ||
primaryPrincipal.email = rawPrimaryPrincipal.email; | ||
primaryPrincipal.status = rawPrimaryPrincipal.status; | ||
const rawPrincipal = googleAuthenticationInformation.principal; | ||
const principal = new _google.GooglePrincipal(); | ||
principal.sub = rawPrincipal.sub; | ||
principal.name = rawPrincipal.name; | ||
principal.familyName = rawPrincipal.familyName; | ||
principal.givenName = rawPrincipal.givenName; | ||
principal.picture = rawPrincipal.picture; | ||
principal.locale = rawPrincipal.locale; | ||
principal.email = rawPrincipal.email; | ||
principal.emailVerified = rawPrincipal.emailVerified; | ||
const authenticationInformation = new _google.GoogleAuthenticationInformation(); | ||
authenticationInformation.primaryPrincipal = primaryPrincipal; | ||
authenticationInformation.principal = principal; | ||
return authenticationInformation; | ||
} | ||
append(information, claims) { | ||
if (information instanceof google_1.GoogleAuthenticationInformation) { | ||
const specifiedInformation = information; | ||
const id = specifiedInformation.primaryPrincipal.id; | ||
claims[Sub] = String(id); | ||
claims[Provider] = 'google'; | ||
return true; | ||
} | ||
return false; | ||
} | ||
async transform(claims) { | ||
const sub = parseInt(claims[Sub], 10); | ||
const googleAuthenticationInformation = await this.kauthApi.getGoogleAuthenticationInformation(sub); | ||
const rawPrimaryPrincipal = googleAuthenticationInformation.primaryPrincipal; | ||
const primaryPrincipal = new base_1.PrimaryPrincipal(); | ||
primaryPrincipal.id = rawPrimaryPrincipal.id; | ||
primaryPrincipal.name = rawPrimaryPrincipal.name; | ||
primaryPrincipal.email = rawPrimaryPrincipal.email; | ||
primaryPrincipal.avatar = rawPrimaryPrincipal.avatar; | ||
primaryPrincipal.status = rawPrimaryPrincipal.status; | ||
const rawPrincipal = googleAuthenticationInformation.principal; | ||
const principal = new google_1.GooglePrincipal(); | ||
principal.sub = rawPrincipal.sub; | ||
principal.name = rawPrincipal.name; | ||
principal.familyName = rawPrincipal.familyName; | ||
principal.givenName = rawPrincipal.givenName; | ||
principal.picture = rawPrincipal.picture; | ||
principal.locale = rawPrincipal.locale; | ||
principal.email = rawPrincipal.email; | ||
principal.emailVerified = rawPrincipal.emailVerified; | ||
const authenticationInformation = new google_1.GoogleAuthenticationInformation(); | ||
authenticationInformation.primaryPrincipal = primaryPrincipal; | ||
authenticationInformation.principal = principal; | ||
return authenticationInformation; | ||
} | ||
} | ||
exports.GoogleAuthenticationInfoManger = GoogleAuthenticationInfoManger; | ||
class MoziAuthenticationInfoManager { | ||
constructor(kauthApi) { | ||
_defineProperty(this, "kauthApi", void 0); | ||
this.kauthApi = kauthApi; | ||
} | ||
append(information, claims) { | ||
if (information instanceof _mozi.MoziAuthenticationInformation) { | ||
const specifiedInformation = information; | ||
const id = specifiedInformation.primaryPrincipal.id; | ||
claims[Sub] = String(id); | ||
claims[Provider] = 'mozi'; | ||
return true; | ||
constructor(kauthApi) { | ||
this.kauthApi = kauthApi; | ||
} | ||
return false; | ||
} | ||
async transform(claims) { | ||
const sub = parseInt(claims[Sub], 10); | ||
const moziAuthenticationInformation = await this.kauthApi.getMoziAuthenticationInformation(sub); | ||
const rawPrimaryPrincipal = moziAuthenticationInformation.primaryPrincipal; | ||
const primaryPrincipal = new _base.PrimaryPrincipal(); | ||
primaryPrincipal.id = rawPrimaryPrincipal.id; | ||
primaryPrincipal.name = rawPrimaryPrincipal.name; | ||
primaryPrincipal.email = rawPrimaryPrincipal.email; | ||
primaryPrincipal.status = rawPrimaryPrincipal.status; | ||
const rawPrincipal = moziAuthenticationInformation.principal; | ||
const principal = new _mozi.MoziPrincipal(); | ||
principal.openid = rawPrincipal.openid; | ||
principal.employeeCode = rawPrincipal.employeeCode; | ||
principal.nickNameCn = rawPrincipal.nickNameCn; | ||
principal.lastName = rawPrincipal.lastName; | ||
principal.account = rawPrincipal.account; | ||
principal.realmId = rawPrincipal.realmId; | ||
principal.realmName = rawPrincipal.realmName; | ||
const authenticationInformation = new _mozi.MoziAuthenticationInformation(); | ||
authenticationInformation.primaryPrincipal = primaryPrincipal; | ||
authenticationInformation.principal = principal; | ||
return authenticationInformation; | ||
} | ||
append(information, claims) { | ||
if (information instanceof mozi_1.MoziAuthenticationInformation) { | ||
const specifiedInformation = information; | ||
const id = specifiedInformation.primaryPrincipal.id; | ||
claims[Sub] = String(id); | ||
claims[Provider] = 'mozi'; | ||
return true; | ||
} | ||
return false; | ||
} | ||
async transform(claims) { | ||
const sub = parseInt(claims[Sub], 10); | ||
const moziAuthenticationInformation = await this.kauthApi.getMoziAuthenticationInformation(sub); | ||
const rawPrimaryPrincipal = moziAuthenticationInformation.primaryPrincipal; | ||
const primaryPrincipal = new base_1.PrimaryPrincipal(); | ||
primaryPrincipal.id = rawPrimaryPrincipal.id; | ||
primaryPrincipal.name = rawPrimaryPrincipal.name; | ||
primaryPrincipal.email = rawPrimaryPrincipal.email; | ||
primaryPrincipal.avatar = rawPrimaryPrincipal.avatar; | ||
primaryPrincipal.status = rawPrimaryPrincipal.status; | ||
const rawPrincipal = moziAuthenticationInformation.principal; | ||
const principal = new mozi_1.MoziPrincipal(); | ||
principal.openid = rawPrincipal.openid; | ||
principal.employeeCode = rawPrincipal.employeeCode; | ||
principal.nickNameCn = rawPrincipal.nickNameCn; | ||
principal.lastName = rawPrincipal.lastName; | ||
principal.account = rawPrincipal.account; | ||
principal.realmId = rawPrincipal.realmId; | ||
principal.realmName = rawPrincipal.realmName; | ||
const authenticationInformation = new mozi_1.MoziAuthenticationInformation(); | ||
authenticationInformation.primaryPrincipal = primaryPrincipal; | ||
authenticationInformation.principal = principal; | ||
return authenticationInformation; | ||
} | ||
} | ||
exports.MoziAuthenticationInfoManager = MoziAuthenticationInfoManager; | ||
exports.MoziAuthenticationInfoManager = MoziAuthenticationInfoManager; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MoziAuthenticationInformation = exports.MoziCredential = exports.MoziPrincipal = void 0; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
class MoziPrincipal { | ||
constructor() { | ||
_defineProperty(this, "account", void 0); | ||
_defineProperty(this, "employeeCode", void 0); | ||
_defineProperty(this, "lastName", void 0); | ||
_defineProperty(this, "namespace", void 0); | ||
_defineProperty(this, "nickNameCn", void 0); | ||
_defineProperty(this, "openid", void 0); | ||
_defineProperty(this, "realmId", void 0); | ||
_defineProperty(this, "realmName", void 0); | ||
} | ||
} | ||
exports.MoziPrincipal = MoziPrincipal; | ||
class MoziCredential { | ||
constructor() { | ||
_defineProperty(this, "accessToken", void 0); | ||
_defineProperty(this, "expiresIn", void 0); | ||
_defineProperty(this, "refreshToken", void 0); | ||
} | ||
} | ||
exports.MoziCredential = MoziCredential; | ||
class MoziAuthenticationInformation { | ||
constructor() { | ||
_defineProperty(this, "primaryPrincipal", void 0); | ||
_defineProperty(this, "principal", void 0); | ||
_defineProperty(this, "credential", void 0); | ||
} | ||
} | ||
exports.MoziAuthenticationInformation = MoziAuthenticationInformation; | ||
exports.MoziAuthenticationInformation = MoziAuthenticationInformation; |
import { AuthenticationInformation } from "./base"; | ||
import { KauthApi } from "src/common/http/kauth"; | ||
import { KauthConfig } from "src/common/config/config"; | ||
import { KauthApi } from "../common/http/kauth"; | ||
import { KauthConfig } from "../common/config/config"; | ||
export interface AuthenticationProvider { | ||
@@ -5,0 +5,0 @@ verify(token: string): Promise<AuthenticationInformation>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.JsonWebTokenAuthenticationProvider = void 0; | ||
var _dayjs = _interopRequireDefault(require("dayjs")); | ||
var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken")); | ||
var _manager = require("./manager"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
const dayjs_1 = __importDefault(require("dayjs")); | ||
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken")); | ||
const manager_1 = require("./manager"); | ||
const Exp = 'exp'; | ||
class JsonWebTokenAuthenticationProvider { | ||
constructor(kauthApi, kauthConfig) { | ||
_defineProperty(this, "authenticationInfoManager", void 0); | ||
_defineProperty(this, "secret", void 0); | ||
_defineProperty(this, "duration", void 0); | ||
this.authenticationInfoManager = new _manager.DelegatedAuthenticationInfoManager(kauthApi); | ||
this.secret = kauthConfig.jwtSecret; | ||
this.duration = kauthConfig.jwtDuration; | ||
} | ||
verify(token) { | ||
try { | ||
const claims = _jsonwebtoken.default.verify(token, this.secret); | ||
return this.authenticationInfoManager.transform(claims); | ||
} catch (error) { | ||
return null; | ||
constructor(kauthApi, kauthConfig) { | ||
this.authenticationInfoManager = new manager_1.DelegatedAuthenticationInfoManager(kauthApi); | ||
this.secret = kauthConfig.jwtSecret; | ||
this.duration = kauthConfig.jwtDuration; | ||
} | ||
} | ||
authenticate(authenticationInformation) { | ||
const exp = (0, _dayjs.default)().add(this.duration, 'second').unix(); | ||
const claims = {}; | ||
claims[Exp] = exp; | ||
if (!this.authenticationInfoManager.append(authenticationInformation, claims)) { | ||
throw new Error('unkown authentication information'); | ||
verify(token) { | ||
try { | ||
const claims = jsonwebtoken_1.default.verify(token, this.secret); | ||
return this.authenticationInfoManager.transform(claims); | ||
} | ||
catch (error) { | ||
return null; | ||
} | ||
} | ||
return _jsonwebtoken.default.sign(claims, this.secret); | ||
} | ||
authenticate(authenticationInformation) { | ||
const exp = dayjs_1.default().add(this.duration, 'second').unix(); | ||
const claims = {}; | ||
claims[Exp] = exp; | ||
if (!this.authenticationInfoManager.append(authenticationInformation, claims)) { | ||
throw new Error('unknown authentication information'); | ||
} | ||
return jsonwebtoken_1.default.sign(claims, this.secret); | ||
} | ||
} | ||
exports.JsonWebTokenAuthenticationProvider = JsonWebTokenAuthenticationProvider; | ||
exports.JsonWebTokenAuthenticationProvider = JsonWebTokenAuthenticationProvider; |
@@ -1,1 +0,2 @@ | ||
"use strict"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -0,8 +1,8 @@ | ||
import { KauthConfig } from '../config/config'; | ||
import { KauthMoziAuthenticationInformation, KauthGoogleAuthenticationInformation, KauthPermission } from './response/kauth'; | ||
import { KauthConfig } from '../config/config'; | ||
export declare class KauthApi { | ||
private kauthOrigin; | ||
private kauthUtil; | ||
constructor({ kauthOrigin }: KauthConfig); | ||
moziAuthorizeURL(callback: string): Promise<string>; | ||
moziAuthenticate(request: { | ||
moziAuthenticate(data: { | ||
code: string; | ||
@@ -13,3 +13,3 @@ redirect_uri: string; | ||
googleAuthorizeURL(callback: string): Promise<string>; | ||
googleAuthenticate(request: { | ||
googleAuthenticate(data: { | ||
code: string; | ||
@@ -20,3 +20,2 @@ redirect_uri: string; | ||
listPermissionsByUserId(id: number): Promise<KauthPermission[]>; | ||
private filterResponse; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.KauthApi = void 0; | ||
var _nodeFetch = _interopRequireDefault(require("node-fetch")); | ||
var _camelcaseKeys = _interopRequireDefault(require("camelcase-keys")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
const axios_1 = __importDefault(require("axios")); | ||
const camelcase_keys_1 = __importDefault(require("camelcase-keys")); | ||
const http_1 = require("../constant/http"); | ||
const kauth_1 = require("./response/kauth"); | ||
class KauthApi { | ||
constructor({ | ||
kauthOrigin | ||
}) { | ||
_defineProperty(this, "kauthOrigin", void 0); | ||
this.kauthOrigin = kauthOrigin || ''; | ||
} | ||
async moziAuthorizeURL(callback) { | ||
const response = await (0, _nodeFetch.default)(this.kauthOrigin + '/api/authenticator/mozi/authorizeUrl?callback=' + callback); | ||
return this.filterResponse(response); | ||
} | ||
async moziAuthenticate(request) { | ||
const url = this.kauthOrigin + '/api/authenticator/mozi/authenticate'; | ||
const response = await (0, _nodeFetch.default)(url, { | ||
method: 'POST', | ||
body: JSON.stringify(request), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
return this.filterResponse(response); | ||
} | ||
async getMoziAuthenticationInformation(id) { | ||
const url = `${this.kauthOrigin}/api/authenticator/mozi/authenticationInformation?id=${id}`; | ||
const response = await (0, _nodeFetch.default)(url); | ||
return this.filterResponse(response); | ||
} | ||
async googleAuthorizeURL(callback) { | ||
const url = `${this.kauthOrigin}/api/authenticator/google/authorizeUrl?callback=${callback}`; | ||
const response = await (0, _nodeFetch.default)(url); | ||
return this.filterResponse(response); | ||
} | ||
async googleAuthenticate(request) { | ||
const url = this.kauthOrigin + '/api/authenticator/google/authenticate'; | ||
const response = await (0, _nodeFetch.default)(url, { | ||
method: 'POST', | ||
body: JSON.stringify(request), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
return this.filterResponse(response); | ||
} | ||
async getGoogleAuthenticationInformation(id) { | ||
const url = `${this.kauthOrigin}/api/authenticator/google/authenticationInformation?id=${id}`; | ||
const response = await (0, _nodeFetch.default)(url); | ||
return this.filterResponse(response); | ||
} | ||
async listPermissionsByUserId(id) { | ||
const url = `${this.kauthOrigin}/api/backend/listPermissionsByUserId?id=${id}`; | ||
const response = await (0, _nodeFetch.default)(url); | ||
return this.filterResponse(response); | ||
} | ||
async filterResponse(response) { | ||
if (response.status != 200) { | ||
throw new Error('internal server error'); | ||
constructor({ kauthOrigin }) { | ||
this.kauthUtil = new KauthUtil(kauthOrigin || ''); | ||
} | ||
const json = await response.json(); | ||
if (json.status !== 0) { | ||
throw new Error(json.message); | ||
async moziAuthorizeURL(callback) { | ||
return this.kauthUtil.get(http_1.MoziAuthorizeUri, { callback }); | ||
} | ||
return (0, _camelcaseKeys.default)(json.body, { | ||
deep: true | ||
}); | ||
} | ||
async moziAuthenticate(data) { | ||
return this.kauthUtil.post(http_1.MoziAuthenticateUri, data); | ||
} | ||
async getMoziAuthenticationInformation(id) { | ||
return this.kauthUtil.get(http_1.MoziAuthenticationInformationUri, { id }); | ||
} | ||
async googleAuthorizeURL(callback) { | ||
return this.kauthUtil.get(http_1.GoogleAuthorizeUri, { callback }); | ||
} | ||
async googleAuthenticate(data) { | ||
return this.kauthUtil.post(http_1.GoogleAuthenticateUri, data); | ||
} | ||
async getGoogleAuthenticationInformation(id) { | ||
return this.kauthUtil.get(http_1.GoogleAuthenticationInformationUri, { id }); | ||
} | ||
async listPermissionsByUserId(id) { | ||
return this.kauthUtil.get(http_1.ListPermissionsByUserIdUri, { id }); | ||
} | ||
} | ||
exports.KauthApi = KauthApi; | ||
exports.KauthApi = KauthApi; | ||
class KauthUtil { | ||
constructor(origin) { | ||
this.origin = origin; | ||
} | ||
async get(path, params) { | ||
const response = await axios_1.default.get(this.origin + path, { | ||
params, | ||
}); | ||
return this.format(response); | ||
} | ||
async post(path, data) { | ||
const response = await axios_1.default.post(this.origin + path, data); | ||
return this.format(response); | ||
} | ||
async del(path, params) { | ||
const response = await axios_1.default.delete(this.origin + path, { | ||
params, | ||
}); | ||
return this.format(response); | ||
} | ||
async format(response) { | ||
if (response.status != 200) { | ||
throw new Error('internal server error'); | ||
} | ||
const json = response.data; | ||
if (json.status !== kauth_1.KauthStatus.OK) { | ||
throw new Error(json.message); | ||
} | ||
return camelcase_keys_1.default(json.body, { | ||
deep: true, | ||
}); | ||
} | ||
} |
@@ -5,9 +5,8 @@ import { KauthConfig } from "../config/config"; | ||
export declare class KflowApi { | ||
private kflowOrigin; | ||
private kflowUtil; | ||
constructor({ kflowOrigin }: KauthConfig); | ||
startProcessInstanceByKey(userId: string, request: KflowCreateProcessRequest): Promise<string>; | ||
completeTaskService(userId: string, request: KflowCompleteTaskServiceRequest): Promise<void>; | ||
startProcessInstanceByKey(userId: string, body: KflowCreateProcessRequest): Promise<string>; | ||
completeTaskService(userId: string, body: KflowCompleteTaskServiceRequest): Promise<void>; | ||
deleteProcessInstance(userId: string, instanceId: string, reason: string): Promise<void>; | ||
getStateMachineAndNodes(userId: string, instanceId: string): Promise<KflowStateMachineAndNodes>; | ||
private filterResponse; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.KflowApi = void 0; | ||
var _nodeFetch = _interopRequireDefault(require("node-fetch")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
const axios_1 = __importDefault(require("axios")); | ||
const http_1 = require("../constant/http"); | ||
const kflow_1 = require("./response/kflow"); | ||
class KflowApi { | ||
constructor({ | ||
kflowOrigin | ||
}) { | ||
_defineProperty(this, "kflowOrigin", void 0); | ||
this.kflowOrigin = kflowOrigin || ''; | ||
} | ||
async startProcessInstanceByKey(userId, request) { | ||
const url = this.kflowOrigin + '/api/kflow/process'; | ||
const response = await (0, _nodeFetch.default)(url, { | ||
method: 'POST', | ||
body: JSON.stringify(request), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-kauth-user-id': userId | ||
} | ||
}); | ||
return this.filterResponse(response); | ||
} | ||
async completeTaskService(userId, request) { | ||
const url = this.kflowOrigin + '/api/kflow/serviceTask'; | ||
const response = await (0, _nodeFetch.default)(url, { | ||
method: 'POST', | ||
body: JSON.stringify(request), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-kauth-user-id': userId | ||
} | ||
}); | ||
return this.filterResponse(response); | ||
} | ||
async deleteProcessInstance(userId, instanceId, reason) { | ||
const url = `${this.kflowOrigin}/api/kflow/process?instanceId=${instanceId}&reason=${reason}`; | ||
const response = await (0, _nodeFetch.default)(url, { | ||
method: 'DELETE', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-kauth-user-id': userId | ||
} | ||
}); | ||
return this.filterResponse(response); | ||
} | ||
async getStateMachineAndNodes(userId, instanceId) { | ||
const url = `${this.kflowOrigin}/api/kflow/stateMachine/instance/${instanceId}`; | ||
const response = await (0, _nodeFetch.default)(url, { | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-kauth-user-id': userId | ||
} | ||
}); | ||
return this.filterResponse(response); | ||
} | ||
async filterResponse(response) { | ||
if (response.status != 200) { | ||
throw new Error('internal server error'); | ||
constructor({ kflowOrigin }) { | ||
this.kflowUtil = new KflowUtil(kflowOrigin || ''); | ||
} | ||
const json = await response.json(); | ||
if (json.status !== 0) { | ||
throw new Error(json.message); | ||
async startProcessInstanceByKey(userId, body) { | ||
return this.kflowUtil.post(userId, http_1.ProcessUri, body); | ||
} | ||
return json.body; | ||
} | ||
async completeTaskService(userId, body) { | ||
return this.kflowUtil.post(userId, http_1.ServiceTaskUri, body); | ||
} | ||
async deleteProcessInstance(userId, instanceId, reason) { | ||
const params = { | ||
instanceId, | ||
reason, | ||
}; | ||
return this.kflowUtil.del(userId, http_1.ProcessUri, params); | ||
} | ||
async getStateMachineAndNodes(userId, instanceId) { | ||
return this.kflowUtil.get(userId, http_1.InstanceUri + instanceId); | ||
} | ||
} | ||
exports.KflowApi = KflowApi; | ||
exports.KflowApi = KflowApi; | ||
class KflowUtil { | ||
constructor(origin) { | ||
this.origin = origin; | ||
} | ||
async get(userId, path, params) { | ||
const response = await axios_1.default.get(this.origin + path, { | ||
params, | ||
headers: { | ||
'x-kauth-user-id': userId, | ||
}, | ||
}); | ||
return this.format(response); | ||
} | ||
async post(userId, path, body) { | ||
const response = await axios_1.default.post(this.origin + path, body, { | ||
headers: { | ||
'x-kauth-user-id': userId, | ||
}, | ||
}); | ||
return this.format(response); | ||
} | ||
async del(userId, path, params) { | ||
const response = await axios_1.default.delete(this.origin + path, { | ||
params, | ||
headers: { | ||
'x-kauth-user-id': userId, | ||
}, | ||
}); | ||
return this.format(response); | ||
} | ||
async format(response) { | ||
if (response.status != 200) { | ||
throw new Error('internal server error'); | ||
} | ||
const json = response.data; | ||
if (json.status !== kflow_1.KflowStatus.OK) { | ||
throw new Error(json.message); | ||
} | ||
return json.body; | ||
} | ||
} |
@@ -1,1 +0,2 @@ | ||
"use strict"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -0,3 +1,13 @@ | ||
export declare enum KauthStatus { | ||
OK = 0, | ||
UnAuthenticated = 10401, | ||
Forbidden = 10403, | ||
InternalServerError = 10500 | ||
} | ||
export declare enum KauthPrincipalStatus { | ||
Normal = 0, | ||
Locked = 1 | ||
} | ||
export interface BasicReponse<T> { | ||
status: number; | ||
status: KauthStatus; | ||
body: T; | ||
@@ -10,3 +20,4 @@ message: string; | ||
email: string; | ||
status: number; | ||
avatar: string; | ||
status: KauthPrincipalStatus; | ||
} | ||
@@ -13,0 +24,0 @@ export interface KauthMoziPrincipal { |
@@ -1,1 +0,15 @@ | ||
"use strict"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.KauthPrincipalStatus = exports.KauthStatus = void 0; | ||
var KauthStatus; | ||
(function (KauthStatus) { | ||
KauthStatus[KauthStatus["OK"] = 0] = "OK"; | ||
KauthStatus[KauthStatus["UnAuthenticated"] = 10401] = "UnAuthenticated"; | ||
KauthStatus[KauthStatus["Forbidden"] = 10403] = "Forbidden"; | ||
KauthStatus[KauthStatus["InternalServerError"] = 10500] = "InternalServerError"; | ||
})(KauthStatus = exports.KauthStatus || (exports.KauthStatus = {})); | ||
var KauthPrincipalStatus; | ||
(function (KauthPrincipalStatus) { | ||
KauthPrincipalStatus[KauthPrincipalStatus["Normal"] = 0] = "Normal"; | ||
KauthPrincipalStatus[KauthPrincipalStatus["Locked"] = 1] = "Locked"; | ||
})(KauthPrincipalStatus = exports.KauthPrincipalStatus || (exports.KauthPrincipalStatus = {})); |
@@ -0,3 +1,7 @@ | ||
export declare enum KflowStatus { | ||
OK = 0, | ||
InternalServerError = 10500 | ||
} | ||
export interface BasicReponse<T> { | ||
status: number; | ||
status: KflowStatus; | ||
body: T; | ||
@@ -4,0 +8,0 @@ message: string; |
@@ -1,1 +0,8 @@ | ||
"use strict"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.KflowStatus = void 0; | ||
var KflowStatus; | ||
(function (KflowStatus) { | ||
KflowStatus[KflowStatus["OK"] = 0] = "OK"; | ||
KflowStatus[KflowStatus["InternalServerError"] = 10500] = "InternalServerError"; | ||
})(KflowStatus = exports.KflowStatus || (exports.KflowStatus = {})); |
@@ -1,1 +0,3 @@ | ||
export { middlewareFactory as KauthEggMiddleware } from './common/middleware/egg'; | ||
export * from './common/config/config'; | ||
export * from './common/http/kauth'; | ||
export * from './authenticator'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "KauthEggMiddleware", { | ||
enumerable: true, | ||
get: function () { | ||
return _egg.middlewareFactory; | ||
} | ||
}); | ||
var _egg = require("./common/middleware/egg"); | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./common/config/config"), exports); | ||
__exportStar(require("./common/http/kauth"), exports); | ||
__exportStar(require("./authenticator"), exports); |
{ | ||
"name": "kauth-sdk-node", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "", | ||
@@ -10,6 +10,9 @@ "main": "index.js", | ||
"scripts": { | ||
"build:types": "tsc -p tsconfig.build.json", | ||
"build": "npm run clean && npm run build:types && gulp", | ||
"clean": "rimraf types lib esm dist", | ||
"watch": "gulp watch" | ||
"build": "npm run clean && tsc", | ||
"type-check": "tsc --noEmit", | ||
"lint": "npm run type-check && eslint . --fix --ext .ts", | ||
"test": "mocha --recursive --require ts-node/register 'test/**/*.ts'", | ||
"coverage": "nyc -t coverage npm run test", | ||
"ci": "npm run lint && npm run coverage" | ||
}, | ||
@@ -19,19 +22,40 @@ "author": "", | ||
"dependencies": { | ||
"axios": "^0.19.2", | ||
"camelcase-keys": "^6.2.2", | ||
"dayjs": "^1.8.29", | ||
"jsonwebtoken": "^8.5.1", | ||
"node-fetch": "^2.6.0" | ||
"jsonwebtoken": "^8.5.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.10.4", | ||
"@babel/plugin-proposal-class-properties": "^7.10.4", | ||
"@babel/preset-env": "^7.10.4", | ||
"@babel/preset-typescript": "^7.10.4", | ||
"@types/node-fetch": "^2.5.7", | ||
"@types/jsonwebtoken": "^8.5.0", | ||
"@types/mocha": "^8.0.0", | ||
"@types/sinon": "^9.0.4", | ||
"@typescript-eslint/eslint-plugin": "^3.7.1", | ||
"@typescript-eslint/parser": "^3.7.1", | ||
"egg": "^2.27.0", | ||
"gulp": "^4.0.2", | ||
"gulp-babel": "^8.0.0", | ||
"eslint": "^7.5.0", | ||
"mocha": "^8.0.1", | ||
"nyc": "^15.1.0", | ||
"rimraf": "^3.0.2", | ||
"sinon": "^9.0.2", | ||
"ts-node": "^8.10.2", | ||
"typescript": "^3.9.6" | ||
}, | ||
"nyc": { | ||
"include": [ | ||
"src/**/*.ts" | ||
], | ||
"extension": [ | ||
".ts" | ||
], | ||
"require": [ | ||
"ts-node/register" | ||
], | ||
"reporter": [ | ||
"lcov", | ||
"text", | ||
"text-summary" | ||
], | ||
"sourceMap": true, | ||
"instrument": true | ||
} | ||
} |
29993
13
737
+ Addedaxios@^0.19.2
+ Addedaxios@0.19.2(transitive)
+ Addedfollow-redirects@1.5.10(transitive)
- Removednode-fetch@^2.6.0
- Removednode-fetch@2.7.0(transitive)
- Removedtr46@0.0.3(transitive)
- Removedwebidl-conversions@3.0.1(transitive)
- Removedwhatwg-url@5.0.0(transitive)