/************************************************************************* * * Troven CONFIDENTIAL * __________________ * * (c) 2017-2020 Troven Ventures Pty Ltd * All Rights Reserved. * * NOTICE: All information contained herein is, and remains * the property of Troven Pty Ltd and its licensors, * if any. The intellectual and technical concepts contained * herein are proprietary to Troven Pty Ltd * and its suppliers and may be covered by International and Regional Patents, * patents in process, and are protected by trade secret or copyright law. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from Troven Pty Ltd. */ import * as assert from "assert"; import * as _ from "lodash"; export class Security { // context: IChassisContext; // openapi: OpenAPI; // schemes: any = {}; security: any = {}; securitySchemes: any = {}; constructor(spec?: any) { // this.context = context; this.defaults(spec); } defaults = function (openapi:any ) { assert(openapi, "missing openapi.options"); this.security = openapi.security || {}; this.securitySchemes = openapi.components.securitySchemes || {}; // this.schemes = this.normalize_schemes(openapi.security); }; findSchemeByName(name: string) { return this.securitySchemes[name]; } findScheme(type: string, scheme: string) { let found = null; for(let _s in this.securitySchemes) { let rule = this.securitySchemes[_s]; if (!found && rule.type == type && rule.scheme == scheme) { found = rule; } } return found; } /** * Flatten the array of named security scheme objects into a named set of scheme objects. * * @param oper_security * @returns {{}} */ private simplify_security_rules(oper_security: any): any { let schemes = {}; for(let sec_i in oper_security) { if (oper_security.hasOwnProperty(sec_i)) { let _security = oper_security[sec_i]; for(let sec_key in _security) { if (_security.hasOwnProperty(sec_key)) { let scheme = this.securitySchemes[sec_key]; assert(scheme, "missing root security scheme: "+sec_key); schemes[sec_key] = _.extend({}, scheme, { scopes: _security[sec_key]} ); } } } } return schemes; }; /** * Uplift an operation by merging normalized security schemes * * @param oper * @returns {any} */ operation(security: any): any { return this.simplify_security_rules( security || this.security ); // use default, if not specified } }