@@ -49,7 +49,43 @@ async function ocsRequest(
4949 throw new Error ( `OCS request failed: ${ method } ${ path } → ${ response . status ( ) } ${ await response . text ( ) } ` )
5050 }
5151
52- return response . json ( ) as Promise < OcsResponse >
52+ const text = await response . text ( )
53+ if ( ! text ) {
54+ return { ocs : { meta : { status : 'ok' , statuscode : response . status ( ) , message : '' } , data : { } } } as OcsResponse
55+ }
56+ return JSON . parse ( text ) as OcsResponse
57+ }
58+
59+ async function libresignRequest (
60+ request : APIRequestContext ,
61+ method : 'GET' | 'POST' | 'PUT' | 'DELETE' ,
62+ path : string ,
63+ adminUser = process . env . NEXTCLOUD_ADMIN_USER ?? 'admin' ,
64+ adminPassword = process . env . NEXTCLOUD_ADMIN_PASSWORD ?? 'admin' ,
65+ jsonBody ?: unknown ,
66+ ) : Promise < unknown > {
67+ const url = `.${ path } `
68+ const auth = 'Basic ' + Buffer . from ( `${ adminUser } :${ adminPassword } ` ) . toString ( 'base64' )
69+ const headers : Record < string , string > = {
70+ Accept : 'application/json' ,
71+ Authorization : auth ,
72+ }
73+ if ( jsonBody !== undefined ) {
74+ headers [ 'Content-Type' ] = 'application/json'
75+ }
76+ const response = await request [ method . toLowerCase ( ) as 'get' | 'post' | 'put' | 'delete' ] ( url , {
77+ headers,
78+ ...( jsonBody !== undefined ? { data : JSON . stringify ( jsonBody ) } : { } ) ,
79+ failOnStatusCode : false ,
80+ } )
81+
82+ if ( ! response . ok ( ) ) {
83+ throw new Error ( `LibreSign API request failed: ${ method } ${ path } → ${ response . status ( ) } ${ await response . text ( ) } ` )
84+ }
85+
86+ const text = await response . text ( )
87+ if ( ! text ) return { }
88+ return JSON . parse ( text )
5389}
5490
5591// ---------------------------------------------------------------------------
@@ -157,16 +193,12 @@ export async function configureOpenSsl(
157193 . filter ( ( [ , val ] ) => val !== undefined )
158194 . map ( ( [ id , value ] ) => ( { id, value } ) )
159195
160- const result = await ocsRequest (
196+ await libresignRequest (
161197 request ,
162198 'POST' ,
163199 '/apps/libresign/api/v1/admin/certificate/openssl' ,
164200 undefined ,
165201 undefined ,
166- undefined ,
167202 { rootCert : { commonName, names : namesArray } } ,
168203 )
169- if ( result . ocs . meta . statuscode !== 200 ) {
170- throw new Error ( `Failed to configure OpenSSL: ${ result . ocs . meta . message } ` )
171- }
172204}
0 commit comments