UNPKG

@julienbenac/ally-gerermesaffaires

Version:
8 lines (7 loc) 2.84 kB
import{getDirname as n}from"@poppinss/utils";var c=n(import.meta.url);async function f(i){let o=await i.createCodemods();await o.defineEnvVariables({GERERMESAFFAIRES_CLIENT_ID:"",GERERMESAFFAIRES_CLIENT_SECRET:"",GERERMESAFFAIRES_CALLBACK_URL:"",GERERMESAFFAIRES_ENV:""}),await o.defineEnvValidations({variables:{GERERMESAFFAIRES_CLIENT_ID:"Env.schema.string()",GERERMESAFFAIRES_CLIENT_SECRET:"Env.schema.string.optional()",GERERMESAFFAIRES_CALLBACK_URL:"Env.schema.string()",GERERMESAFFAIRES_ENV:'Env.schema.enum(["production", "sandbox"] as const)'},leadingComment:"Variables for configuring @julienbenac/ally-gerermesaffaires"})}import{Oauth2Driver as p}from"@adonisjs/ally";var a=class extends p{constructor(e,t){super(e,t);this.config=t;switch(t.env){case"production":this.authorizeUrl="https://gerermesaffaires.com/openid/v1/authorize",this.accessTokenUrl="https://gerermesaffaires.com/openid/v1/token",this.userInfoUrl="https://gerermesaffaires.com/openid/v1/userinfo";break;case"sandbox":this.authorizeUrl="https://sandbox.gerermesaffaires.com/openid/v1/authorize",this.accessTokenUrl="https://sandbox.gerermesaffaires.com/openid/v1/token",this.userInfoUrl="https://sandbox.gerermesaffaires.com/openid/v1/userinfo";break}this.loadState()}authorizeUrl;accessTokenUrl;userInfoUrl;stateCookieName="gerermesaffaires_oauth_state";stateParamName="state";codeParamName="code";errorParamName="error";scopeParamName="scope";scopesSeparator=" ";#e(){if(this.isStateless)return;let e=this.getState();return this.ctx.response.encryptedCookie(this.stateCookieName,e,{sameSite:!1,httpOnly:!0}),e}configureRedirectRequest(e){e.scopes(this.config.scopes||["openid","collaborator","profile","email"]);let t=this.#e();t&&e.param(this.stateParamName,t),e.param("response_type","code")}async redirect(e){let t=await this.redirectUrl(r=>{typeof e=="function"&&e(r)});this.ctx.response.redirect(t)}getAuthenticatedRequest(e,t){let r=this.httpClient(e);return r.header("Authorization",`Bearer ${t}`),r.header("Accept","application/json"),r.parseAs("json"),r}async getUserInfo(e,t){let r=this.getAuthenticatedRequest(this.userInfoUrl,e);typeof t=="function"&&t(r);let s=await r.get();return{id:s.sub,nickName:s.given_name,name:s.family_name,email:s.email,emailVerificationState:s.email_verified?"verified":"unverified",avatarUrl:s.picture,original:s}}accessDenied(){let e=this.getError();return e?e==="access_denied":!1}async user(e){let{id_token:t,...r}=await this.accessToken(e);return{...await this.getUserInfo(r.token,e),token:{...r,idToken:t}}}async userFromToken(e,t){return{...await this.getUserInfo(e,t),token:{token:e,type:"bearer"}}}};function A(i){return o=>new a(o,i)}export{f as configure,A as gerermesaffaires,c as stubsRoot}; /** * @julienbenac/ally-gerermesaffaires * * @author Julien Benac <contact@julienbenac.fr> * @license MIT */