govbr-auth
Version:
Biblioteca de autenticação GOV.BR não oficial para aplicações Node.js | Unofficial GOV.BR authentication library for Node.js applications
2 lines (1 loc) • 4.8 kB
JavaScript
;var N=Object.create;var c=Object.defineProperty,w=Object.defineProperties,T=Object.getOwnPropertyDescriptor,B=Object.getOwnPropertyDescriptors,$=Object.getOwnPropertyNames,y=Object.getOwnPropertySymbols,z=Object.getPrototypeOf,R=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable;var S=(t,e,o)=>e in t?c(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,E=(t,e)=>{for(var o in e||(e={}))R.call(e,o)&&S(t,o,e[o]);if(y)for(var o of y(e))k.call(e,o)&&S(t,o,e[o]);return t},A=(t,e)=>w(t,B(e));var L=(t,e)=>{for(var o in e)c(t,o,{get:e[o],enumerable:!0})},_=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of $(e))!R.call(t,i)&&i!==o&&c(t,i,{get:()=>e[i],enumerable:!(r=T(e,i))||r.enumerable});return t};var G=(t,e,o)=>(o=t!=null?N(z(t)):{},_(e||!t||!t.__esModule?c(o,"default",{value:t,enumerable:!0}):o,t)),O=t=>_(c({},"__esModule",{value:!0}),t);var p=(t,e,o)=>new Promise((r,i)=>{var x=s=>{try{b(o.next(s))}catch(v){i(v)}},I=s=>{try{b(o.throw(s))}catch(v){i(v)}},b=s=>s.done?r(s.value):Promise.resolve(s.value).then(x,I);b((o=o.apply(t,e)).next())});var D={};L(D,{GovBRAuth:()=>m,GovBRAuthError:()=>n,base64URLEncode:()=>h,generateCodeChallenge:()=>u,generateCodeVerifier:()=>l,generateNonce:()=>d,generateState:()=>g});module.exports=O(D);var C=G(require("axios"));var P={staging:{authorize:"https://sso.staging.acesso.gov.br/authorize",token:"https://sso.staging.acesso.gov.br/token",userinfo:"https://sso.staging.acesso.gov.br/userinfo",logout:"https://sso.staging.acesso.gov.br/logout",confiabilidades:"https://api.staging.acesso.gov.br/confiabilidades/v3"},production:{authorize:"https://sso.acesso.gov.br/authorize",token:"https://sso.acesso.gov.br/token",userinfo:"https://sso.acesso.gov.br/userinfo",logout:"https://sso.acesso.gov.br/logout",confiabilidades:"https://api.acesso.gov.br/confiabilidades/v3"}},U=["openid","email","profile","govbr_confiabilidades"];var n=class extends Error{constructor(o,r,i,x){super(o);this.code=r;this.statusCode=i;this.originalError=x;this.name="GovBRAuthError",Error.captureStackTrace(this,this.constructor)}};var a=require("crypto");function d(){return(0,a.randomBytes)(16).toString("hex")}function g(){return(0,a.randomBytes)(16).toString("hex")}function l(){return(0,a.randomBytes)(32).toString("base64url")}function u(t){return(0,a.createHash)("sha256").update(t).digest("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function h(t){return Buffer.from(t).toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}var f=class{constructor(e){if(!e.clientId||!e.clientSecret||!e.redirectUri)throw new Error("Missing required configuration parameters");this.config=A(E({},e),{environment:e.environment||"production",scopes:e.scopes||U}),this.endpoints=P[this.config.environment],this.client=C.default.create({timeout:1e4,headers:{"Content-Type":"application/json"}}),this.setupAxiosInterceptors()}setupAxiosInterceptors(){this.client.interceptors.response.use(e=>e,e=>{var o;throw C.default.isAxiosError(e)?new n(e.message,"API_ERROR",(o=e.response)==null?void 0:o.status,e):e})}generateAuthorizationUrl(e={}){let o=l(),r=u(o),i=new URLSearchParams({response_type:e.responseType||"code",client_id:this.config.clientId,scope:this.config.scopes.join(" "),redirect_uri:this.config.redirectUri,nonce:e.nonce||d(),state:e.state||g(),code_challenge:e.codeChallenge||r,code_challenge_method:e.codeChallengeMethod||"S256"});return`${this.endpoints.authorize}?${i.toString()}`}getTokens(e,o){return p(this,null,function*(){let r=h(`${this.config.clientId}:${this.config.clientSecret}`);return(yield this.client.post(this.endpoints.token,new URLSearchParams({grant_type:"authorization_code",code:e,redirect_uri:this.config.redirectUri,code_verifier:o}),{headers:{"Content-Type":"application/x-www-form-urlencoded",Authorization:`Basic ${r}`}})).data})}getUserInfo(e){return p(this,null,function*(){return(yield this.client.get(this.endpoints.userinfo,{headers:{Authorization:`Bearer ${e}`}})).data})}getConfiabilidadeNiveis(e,o){return p(this,null,function*(){return(yield this.client.get(`${this.endpoints.confiabilidades}/contas/${o}/niveis`,{params:{"response-type":"ids"},headers:{Authorization:`Bearer ${e}`}})).data})}getConfiabilidadeSelos(e,o){return p(this,null,function*(){return(yield this.client.get(`${this.endpoints.confiabilidades}/contas/${o}/confiabilidades`,{params:{"response-type":"ids"},headers:{Authorization:`Bearer ${e}`}})).data})}generateLogoutUrl(e){let o=new URLSearchParams({post_logout_redirect_uri:e});return`${this.endpoints.logout}?${o.toString()}`}};var m=class extends f{constructor(e){super(e)}};0&&(module.exports={GovBRAuth,GovBRAuthError,base64URLEncode,generateCodeChallenge,generateCodeVerifier,generateNonce,generateState});