@foxiko/client
Advanced tools
Comparing version 1.0.78 to 1.0.79
const {IdentityServer} = require("./IdentityServer"); | ||
const {Client} = require("./entities/Client"); | ||
const {IDENTITY} = require("../Networking"); | ||
class FoxikoIdentity { | ||
#client; | ||
client; | ||
constructor(client) { | ||
this.#client = client; | ||
this.client = client; | ||
} | ||
createIdentityServer({authorizeCallback}) { | ||
return new IdentityServer(this.#client, {authorizeCallback}); | ||
return new IdentityServer(this.client, {authorizeCallback}, this); | ||
} | ||
async create(scopes = [], data = {}) { | ||
const result = await this.client.request() | ||
.url(`${IDENTITY.CLIENT.CREATE}`) | ||
.responseType().json() | ||
.contentType().json() | ||
.body({ | ||
scopes, | ||
data | ||
}) | ||
.send(); | ||
if(result['error']) { | ||
throw result; | ||
} | ||
const client = new Client(result); | ||
client._handler(this); | ||
return client; | ||
} | ||
async createAccessToken(client, { | ||
token, | ||
refresh_token, | ||
token_type, | ||
scope, | ||
expires, | ||
}) { | ||
if(client instanceof Client) { | ||
client = client.id; | ||
} | ||
const result = await this.client.request() | ||
.url(`${IDENTITY.CLIENT.CREATE_ACCESS_TOKEN(client)}`) | ||
.responseType().json() | ||
.contentType().json() | ||
.body({ | ||
token, | ||
refresh_token, | ||
token_type, | ||
scope, | ||
expires, | ||
}) | ||
.send(); | ||
if(result['error']) { | ||
console.error("Cannot save access token", result); | ||
} | ||
} | ||
async updateAccessToken(client, { | ||
token, | ||
refresh_token, | ||
token_type, | ||
scope, | ||
expires, | ||
}) { | ||
if(client instanceof Client) { | ||
client = client.id; | ||
} | ||
const result = await this.client.request() | ||
.url(`${IDENTITY.CLIENT.UPDATE_ACCESS_TOKEN(client)}`) | ||
.responseType().json() | ||
.contentType().json() | ||
.body({ | ||
token, | ||
refresh_token, | ||
token_type, | ||
scope, | ||
expires, | ||
}) | ||
.send(); | ||
if(result['error']) { | ||
console.error("Cannot save access token", result); | ||
} | ||
} | ||
async findAccessToken(token, tokenType) { | ||
const result = await this.client.request() | ||
.url(`${IDENTITY.CLIENT.FIND_ACCESS_TOKEN}`) | ||
.responseType().json() | ||
.contentType().json() | ||
.body({ | ||
token, | ||
token_type: tokenType, | ||
}) | ||
.send(); | ||
if(result['error']) { | ||
throw result; | ||
} | ||
return result; | ||
} | ||
async get(id) { | ||
const data = await this.client.request() | ||
.url(`${IDENTITY.CLIENT.GET(id)}`) | ||
.responseType().json() | ||
.send(); | ||
if(data['error']) { | ||
throw data; | ||
} | ||
const client = new Client(data); | ||
client._handler(this); | ||
return client; | ||
} | ||
async edit(client) { | ||
if(!(client instanceof Client)) { | ||
throw "client must be instanceof Client"; | ||
} | ||
const data = await this.client.request() | ||
.url(`${IDENTITY.CLIENT.EDIT(client.id)}`) | ||
.responseType().json() | ||
.contentType().json() | ||
.body(client.toArray()) | ||
.send(); | ||
if(data['error']) { | ||
throw data; | ||
} | ||
client = new Client(data); | ||
client._handler(this); | ||
return client; | ||
} | ||
async validate(client_id, client_secret, scope) { | ||
if(!(client instanceof Client)) { | ||
throw "client must be instanceof Client"; | ||
} | ||
const data = await this.client.request() | ||
.url(`${IDENTITY.CLIENT.EDIT(client.id)}`) | ||
.responseType().json() | ||
.contentType().json() | ||
.body(client.toArray()) | ||
.send(); | ||
if(data['error']) { | ||
throw data; | ||
} | ||
client = new Client(data); | ||
client._handler(this); | ||
return client; | ||
} | ||
} | ||
module.exports.FoxikoIdentity = FoxikoIdentity; |
@@ -1,2 +0,2 @@ | ||
const {IDENTITY} = require("../Networking"); | ||
const {OAuthCredentials} = require("@foxiko/oauth/OAuthServer"); | ||
const {OAuthServer} = require("@foxiko/oauth"); | ||
@@ -7,66 +7,85 @@ | ||
async _validateClientId(clientId) { | ||
const data = await this.client.request() | ||
.url(`${IDENTITY.SECURITY.VALIDATION}`) | ||
.body({ | ||
client_id: clientId | ||
}) | ||
.responseType().json() | ||
.send(); | ||
if(data['valid'] === undefined) { | ||
throw data['error']; | ||
try { | ||
return await this.handler.get(clientId); | ||
} catch (e) { | ||
return null; | ||
} | ||
return data['valid']; | ||
} | ||
async _validateCredentials(clientId, clientSecret) { | ||
const data = await this.client.request() | ||
.url(`${IDENTITY.SECURITY.VALIDATION}`) | ||
.body({ | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
}) | ||
.responseType().json() | ||
.send(); | ||
if(data['valid'] === undefined) { | ||
throw data['error']; | ||
} | ||
if(!data['valid']) { | ||
return false; | ||
} | ||
return data['data']; | ||
return await this.handler.validate(clientId, clientSecret); | ||
} | ||
async _validateCredentialsScope(clientId, scope) { | ||
const data = await this.client.request() | ||
.url(`${IDENTITY.SECURITY.VALIDATION}`) | ||
.body({ | ||
client_id: clientId, | ||
scope: scope, | ||
}) | ||
.responseType().json() | ||
.send(); | ||
if(data['valid'] === undefined) { | ||
throw data['error']; | ||
} | ||
console.log(data); | ||
if(!data['valid']) { | ||
try { | ||
const client = await this.handler.get(clientId); | ||
if(!Array.isArray(scope)) { | ||
scope = scope.split(" "); | ||
} | ||
if(scope.length !== client.scope.length) { | ||
return false; | ||
} | ||
for(let s of scope) { | ||
if(!client.scope.includes(s)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
return data['scope'].join(" "); | ||
} | ||
async _registerToken(credentials) { | ||
return null; | ||
await this.handler.createAccessToken(credentials.data.id, { | ||
token: credentials.token, | ||
refresh_token: credentials.refreshToken, | ||
token_type: credentials.tokenType, | ||
scope: credentials.scope, | ||
expires: credentials.expires, | ||
}); | ||
} | ||
async _updateToken(credentials) { | ||
return null; | ||
await this.handler.updateAccessToken(credentials.data.id, { | ||
token: credentials.token, | ||
refresh_token: credentials.refreshToken, | ||
token_type: credentials.tokenType, | ||
scope: credentials.scope, | ||
expires: credentials.expires, | ||
}); | ||
} | ||
async _registerAuthorization(authorization) { | ||
return null; | ||
throw "This feature is currently not supported"; | ||
} | ||
async _updateAuthorization(authorization) { | ||
return null; | ||
throw "This feature is currently not supported"; | ||
} | ||
async _findToken(accessToken) { | ||
async _findToken(tokenType, token) { | ||
try { | ||
const result = await this.handler.findAccessToken(token, tokenType); | ||
const credentials = new OAuthCredentials({ | ||
client_id: result.client.client_id, | ||
client_secret: null, | ||
data: result.client, | ||
request_data: {}, | ||
scope: result.scope.join(" "), | ||
handler: this, | ||
announce: false | ||
}); | ||
credentials.scope = result.scope; | ||
credentials.expires = result.expires; | ||
credentials.token = result.token; | ||
credentials.tokenType = result.token_type; | ||
credentials.refreshToken = result.refresh_token; | ||
return credentials; | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
async _findAuthorization(code) { | ||
throw "This feature is currently not supported"; | ||
} | ||
@@ -93,4 +112,5 @@ async _findRefreshToken(refreshToken) { | ||
* @param authorizeCallback | ||
* @param handler | ||
*/ | ||
constructor(client, {authorizeCallback = undefined}) { | ||
constructor(client, {authorizeCallback = undefined}, handler) { | ||
super({ | ||
@@ -106,2 +126,3 @@ authorizeCallback, | ||
this.client = client; | ||
this.handler = handler; | ||
@@ -108,0 +129,0 @@ this.on("new_access_token", credentials => this._registerToken(credentials)); |
@@ -7,3 +7,2 @@ const schema = process.env['LOCAL'] === "true" ? "http" : "https"; | ||
GET: `${schema}://${domain}/${version}/profile/get`, | ||
} | ||
@@ -81,3 +80,2 @@ const finance = { | ||
} | ||
const cloud = { | ||
@@ -131,3 +129,2 @@ CONTAINER: { | ||
} | ||
const communication = { | ||
@@ -142,4 +139,12 @@ PDF: { | ||
} | ||
const identity = { | ||
CLIENT: { | ||
CREATE: `${schema}://${domain}/${version}/identity/clients/create`, | ||
GET: id => `${schema}://${domain}/${version}/identity/client/${id}/get`, | ||
EDIT: id => `${schema}://${domain}/${version}/identity/client/${id}/edit`, | ||
VALIDATE: id => `${schema}://${domain}/${version}/identity/client/${id}/validate`, | ||
CREATE_ACCESS_TOKEN: id => `${schema}://${domain}/${version}/identity/client/${id}/access-tokens/create`, | ||
UPDATE_ACCESS_TOKEN: id => `${schema}://${domain}/${version}/identity/client/${id}/access-tokens/update`, | ||
FIND_ACCESS_TOKEN: `${schema}://${domain}/${version}/identity/clients/access-tokens/find`, | ||
}, | ||
SECURITY: { | ||
@@ -154,3 +159,2 @@ VALIDATION: `${schema}://${domain}/${version}/identity/security/clients/validate`, | ||
}; | ||
const authorization = { | ||
@@ -157,0 +161,0 @@ TOKEN: `${schema}://${domain}/${version}/token`, |
{ | ||
"name": "@foxiko/client", | ||
"version": "1.0.78", | ||
"version": "1.0.79", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
185226
60
3822