@@ -8,7 +8,7 @@ import { VERSION } from "./meta.js";
8
8
* @param {string | null } [dopplerConfig]
9
9
* @returns {() => Promise<Record<string, Record>> }
10
10
*/
11
- async function fetch ( dopplerToken , dopplerProject , dopplerConfig ) {
11
+ export async function fetch ( dopplerToken , dopplerProject , dopplerConfig ) {
12
12
return new Promise ( function ( resolve , reject ) {
13
13
const encodedAuthData = Buffer . from ( `${ dopplerToken } :` ) . toString ( "base64" ) ;
14
14
const authHeader = `Basic ${ encodedAuthData } ` ;
@@ -54,4 +54,60 @@ async function fetch(dopplerToken, dopplerProject, dopplerConfig) {
54
54
} ) ;
55
55
}
56
56
57
- export default fetch ;
57
+ /**
58
+ * Exchange an OIDC token for a short lived Doppler service account token
59
+ * @param {string } identityId
60
+ * @param {string } oidcToken
61
+ * @returns {() => Promise<string> }
62
+ */
63
+ export async function oidcAuth ( identityId , oidcToken ) {
64
+ return new Promise ( function ( resolve , reject ) {
65
+ const userAgent = `secrets-fetch-github-action/${ VERSION } ` ;
66
+
67
+ const url = new URL ( "https://api.doppler.com/v3/auth/oidc" ) ;
68
+ const body = JSON . stringify ( {
69
+ identity : identityId ,
70
+ token : oidcToken
71
+ } ) ;
72
+
73
+ const request = https
74
+ . request (
75
+ url . href ,
76
+ {
77
+ headers : {
78
+ "user-agent" : userAgent ,
79
+ "accepts" : "application/json" ,
80
+ "Content-Type" : "application/json" ,
81
+ "Content-Length" : body . length ,
82
+ } ,
83
+ method : 'POST'
84
+ } ,
85
+ ( res ) => {
86
+ let payload = "" ;
87
+ res . on ( "data" , ( data ) => ( payload += data ) ) ;
88
+ res . on ( "end" , ( ) => {
89
+ if ( res . statusCode === 200 ) {
90
+ resolve ( JSON . parse ( payload ) . token ) ;
91
+ } else {
92
+ try {
93
+ const error = JSON . parse ( payload ) . messages . join ( " " ) ;
94
+ reject ( new Error ( `Doppler API Error: ${ error } ` ) ) ;
95
+ } catch ( error ) {
96
+ // In the event an upstream issue occurs and no JSON payload is supplied
97
+ reject ( new Error ( `Doppler API Error: ${ res . statusCode } ${ res . statusMessage } ` ) ) ;
98
+ }
99
+ }
100
+ } ) ;
101
+ }
102
+ ) ;
103
+
104
+ request
105
+ . on ( "error" , ( error ) => {
106
+ reject ( new Error ( `Doppler API Error: ${ error } ` ) ) ;
107
+ } ) ;
108
+
109
+ request . write ( body ) ;
110
+
111
+ request . end ( )
112
+ } ) ;
113
+ }
0 commit comments