@nu-art/google-services
Version:
google-services
41 lines (40 loc) • 1.61 kB
JavaScript
;
/**
* Created by tacb0ss on 07/05/2018.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModuleBE_Auth = exports.ModuleBE_Auth_Class = void 0;
const ts_common_1 = require("@nu-art/ts-common");
const google_auth_library_1 = require("google-auth-library");
class ModuleBE_Auth_Class extends ts_common_1.Module {
constructor() {
super();
this.setDefaultConfig({ auth: {} });
}
getAuth(authKey, scopes, version = 'v2', clientOptions) {
const authConfig = this.getAuthConfig(authKey);
let opts;
if (typeof authConfig === 'string') {
opts = { keyFile: authConfig, scopes, clientOptions };
}
else {
opts = { credentials: authConfig, scopes, clientOptions };
}
return { version, auth: new google_auth_library_1.GoogleAuth(opts) };
}
getAuthConfig(authKey) {
const projectAuth = this.config.auth[authKey];
if (!projectAuth)
throw new ts_common_1.ImplementationMissingException(`Config of authKey: ${authKey} was not found`);
return projectAuth;
}
async getJWT(authKey, scopes) {
const authConfig = this.getAuthConfig(authKey);
if (typeof authConfig === 'string') {
return new google_auth_library_1.JWT({ keyFile: authConfig, scopes }).authorize();
}
throw new ts_common_1.NotImplementedYetException('cannot create a JWT from a raw credentials.. need path to file');
}
}
exports.ModuleBE_Auth_Class = ModuleBE_Auth_Class;
exports.ModuleBE_Auth = new ModuleBE_Auth_Class();