@@ -30,8 +30,6 @@ export interface ProConnectProfile {
3030 exp ?: number ;
3131}
3232
33-
34-
3533export function ProConnectProvider < P extends ProConnectProfile > (
3634 options ?: OAuthUserConfig < P > ,
3735) : OAuthConfig < P > {
@@ -56,70 +54,91 @@ export function ProConnectProvider<P extends ProConnectProfile>(
5654 userinfo : {
5755 url : proconnect . userinfo_endpoint ,
5856 async request ( { tokens } ) {
59- if ( ! tokens . access_token ) throw new Error ( "No access token" ) ;
57+ if ( ! tokens . access_token ) {
58+ logger . error ( "❌ Pas d'access_token disponible pour userinfo" ) ;
59+ throw new Error ( "No access token" ) ;
60+ }
6061
61- const response = await fetch ( proconnect . userinfo_endpoint , {
62- headers : {
63- Authorization : `Bearer ${ tokens . access_token } ` ,
64- Accept : "application/json" ,
65- } ,
66- cache : "no-store" ,
67- } ) ;
62+ try {
63+ const response = await fetch ( proconnect . userinfo_endpoint , {
64+ headers : {
65+ Authorization : `Bearer ${ tokens . access_token } ` ,
66+ Accept : "application/json" ,
67+ } ,
68+ cache : "no-store" ,
69+ } ) ;
6870
69- if ( ! response . ok ) {
70- const text = await response . text ( ) ;
71- logger . error (
72- { status : response . status , body : text } ,
73- "ProConnect userinfo error" ,
74- ) ;
75- throw new Error ( `ProConnect userinfo failed: ${ response . status } ` ) ;
76- }
71+ if ( ! response . ok ) {
72+ const text = await response . text ( ) ;
73+ logger . error (
74+ {
75+ status : response . status ,
76+ statusText : response . statusText ,
77+ url : proconnect . userinfo_endpoint ,
78+ body : text ,
79+ } ,
80+ "❌ Userinfo request failed" ,
81+ ) ;
82+ throw new Error ( `ProConnect userinfo failed: ${ response . status } ` ) ;
83+ }
7784
78- const contentType = response . headers . get ( "content-type" ) || "" ;
79- const rawBody = await response . text ( ) ;
85+ const contentType = response . headers . get ( "content-type" ) || "" ;
86+ const rawBody = await response . text ( ) ;
8087
81- if ( contentType . includes ( "jwt" ) || rawBody . startsWith ( "ey" ) ) {
82- const parts = rawBody . trim ( ) . split ( "." ) ;
83- if ( parts . length !== 3 ) throw new Error ( "Invalid JWT format" ) ;
88+ if ( contentType . includes ( "jwt" ) || rawBody . startsWith ( "ey" ) ) {
89+ const parts = rawBody . trim ( ) . split ( "." ) ;
90+ if ( parts . length !== 3 ) throw new Error ( "Invalid JWT format" ) ;
8491
85- let payload = parts [ 1 ] ;
86- payload += "=" . repeat ( ( 4 - ( payload . length % 4 ) ) % 4 ) ;
87- const decoded = JSON . parse (
88- Buffer . from ( payload , "base64url" ) . toString ( "utf-8" ) ,
92+ let payload = parts [ 1 ] ;
93+ payload += "=" . repeat ( ( 4 - ( payload . length % 4 ) ) % 4 ) ;
94+ const decoded = JSON . parse (
95+ Buffer . from ( payload , "base64url" ) . toString ( "utf-8" ) ,
96+ ) ;
97+ return decoded ;
98+ }
99+
100+ return JSON . parse ( rawBody ) ;
101+ } catch ( error ) {
102+ logger . error (
103+ {
104+ error : error instanceof Error ? error . message : String ( error ) ,
105+ url : proconnect . userinfo_endpoint ,
106+ } ,
107+ "❌ Erreur lors de la requête userinfo" ,
89108 ) ;
90- return decoded ;
109+ throw error ;
91110 }
92-
93- return JSON . parse ( rawBody ) ;
94111 } ,
95112 } ,
96113 checks : [ "pkce" , "state" ] ,
97114 async profile ( profile : ProConnectProfile ) {
98- logger . info ( { profile } , "ProConnect profile reçu" ) ;
99-
100115 return {
101116 id : profile . sub ,
102117 email : profile . email ,
103118 emailVerified : profile . email_verified ?? false ,
104- name : `${ profile . given_name ?? "" } ${ profile . usual_name ?? "" } ` . trim ( ) || null ,
119+ name :
120+ `${ profile . given_name ?? "" } ${ profile . usual_name ?? "" } ` . trim ( ) ||
121+ null ,
105122 given_name : profile . given_name ?? null ,
106123 family_name : profile . usual_name ?? null ,
107124 phone_number : profile . phone_number
108125 ? profile . phone_number . replace ( / [ . \- \s ] / g, "" )
109126 : null ,
110127 siret : profile . siret || null ,
111- organization : profile . siret ? {
112- id : parseInt ( profile . siret , 10 ) ,
113- label : null ,
114- siren : profile . siret . substring ( 0 , 9 ) ,
115- siret : profile . siret ,
116- is_collectivite_territoriale : false ,
117- is_external : false ,
118- is_service_public : false ,
119- } : undefined ,
128+ organization : profile . siret
129+ ? {
130+ id : parseInt ( profile . siret , 10 ) ,
131+ label : null ,
132+ siren : profile . siret . substring ( 0 , 9 ) ,
133+ siret : profile . siret ,
134+ is_collectivite_territoriale : false ,
135+ is_external : false ,
136+ is_service_public : false ,
137+ }
138+ : undefined ,
120139 raw : profile ,
121140 } ;
122141 } ,
123142 ...options ,
124143 } ;
125- }
144+ }
0 commit comments