UNPKG

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.1 kB
var P=Object.defineProperty,U=Object.defineProperties;var I=Object.getOwnPropertyDescriptors;var C=Object.getOwnPropertySymbols;var N=Object.prototype.hasOwnProperty,w=Object.prototype.propertyIsEnumerable;var x=(t,e,o)=>e in t?P(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,y=(t,e)=>{for(var o in e||(e={}))N.call(e,o)&&x(t,o,e[o]);if(C)for(var o of C(e))w.call(e,o)&&x(t,o,e[o]);return t},S=(t,e)=>U(t,I(e));var n=(t,e,o)=>new Promise((i,s)=>{var v=r=>{try{p(o.next(r))}catch(d){s(d)}},_=r=>{try{p(o.throw(r))}catch(d){s(d)}},p=r=>r.done?i(r.value):Promise.resolve(r.value).then(v,_);p((o=o.apply(t,e)).next())});import A from"axios";var R={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"}},E=["openid","email","profile","govbr_confiabilidades"];var a=class extends Error{constructor(o,i,s,v){super(o);this.code=i;this.statusCode=s;this.originalError=v;this.name="GovBRAuthError",Error.captureStackTrace(this,this.constructor)}};import{createHash as T,randomBytes as g}from"crypto";function l(){return g(16).toString("hex")}function u(){return g(16).toString("hex")}function h(){return g(32).toString("base64url")}function f(t){return T("sha256").update(t).digest("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function m(t){return Buffer.from(t).toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}var c=class{constructor(e){if(!e.clientId||!e.clientSecret||!e.redirectUri)throw new Error("Missing required configuration parameters");this.config=S(y({},e),{environment:e.environment||"production",scopes:e.scopes||E}),this.endpoints=R[this.config.environment],this.client=A.create({timeout:1e4,headers:{"Content-Type":"application/json"}}),this.setupAxiosInterceptors()}setupAxiosInterceptors(){this.client.interceptors.response.use(e=>e,e=>{var o;throw A.isAxiosError(e)?new a(e.message,"API_ERROR",(o=e.response)==null?void 0:o.status,e):e})}generateAuthorizationUrl(e={}){let o=h(),i=f(o),s=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||l(),state:e.state||u(),code_challenge:e.codeChallenge||i,code_challenge_method:e.codeChallengeMethod||"S256"});return`${this.endpoints.authorize}?${s.toString()}`}getTokens(e,o){return n(this,null,function*(){let i=m(`${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 ${i}`}})).data})}getUserInfo(e){return n(this,null,function*(){return(yield this.client.get(this.endpoints.userinfo,{headers:{Authorization:`Bearer ${e}`}})).data})}getConfiabilidadeNiveis(e,o){return n(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 n(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 b=class extends c{constructor(e){super(e)}};export{b as GovBRAuth,a as GovBRAuthError,m as base64URLEncode,f as generateCodeChallenge,h as generateCodeVerifier,l as generateNonce,u as generateState};