Skip to content

Commit eaf6458

Browse files
fix: go-feature-flag provider add authentication header (open-feature#317)
Signed-off-by: Thomas Poignant <[email protected]>
1 parent 5e4f876 commit eaf6458

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

libs/providers/go-feature-flag/src/lib/go-feature-flag-provider.spec.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,16 @@ describe('GoFeatureFlagProvider', () => {
133133
);
134134
});
135135
});
136-
it('should throw an error if invalid api key is propvided', async () => {
136+
it('should throw an error if invalid api key is provided', async () => {
137137
const flagName = 'unauthorized';
138138
const targetingKey = 'user-key';
139139
const dns = `${endpoint}v1/feature/${flagName}/eval`;
140+
const apiKey = 'invalid-api-key';
140141

141142
axiosMock.onPost(dns).reply(401, {} as GoFeatureFlagProxyResponse<string>);
142143

143-
await goff
144+
const authenticatedGoff = new GoFeatureFlagProvider({ endpoint, apiKey });
145+
await authenticatedGoff
144146
.resolveStringEvaluation(flagName, 'sdk-default', { targetingKey })
145147
.catch((err) => {
146148
expect(err).toBeInstanceOf(Unauthorized);
@@ -149,6 +151,33 @@ describe('GoFeatureFlagProvider', () => {
149151
);
150152
});
151153
});
154+
155+
it('should be valid with an API key provided', async () => {
156+
const flagName = 'random-flag';
157+
const targetingKey = 'user-key';
158+
const dns = `${endpoint}v1/feature/${flagName}/eval`;
159+
const apiKey = 'valid-api-key';
160+
161+
axiosMock.onPost(dns).reply(200, {
162+
value: true,
163+
variationType: 'trueVariation',
164+
reason: StandardResolutionReasons.TARGETING_MATCH,
165+
failed: false,
166+
trackEvents: true,
167+
version: '1.0.0',
168+
} as GoFeatureFlagProxyResponse<boolean>);
169+
170+
const authenticatedGoff = new GoFeatureFlagProvider({ endpoint, apiKey });
171+
await authenticatedGoff
172+
.resolveBooleanEvaluation(flagName, false, { targetingKey })
173+
.then((res) => {
174+
expect(res).toEqual({
175+
reason: StandardResolutionReasons.TARGETING_MATCH,
176+
value: true,
177+
variant: 'trueVariation',
178+
} as ResolutionDetails<boolean>);
179+
});
180+
});
152181
});
153182

154183
describe('resolveBooleanEvaluation', () => {

libs/providers/go-feature-flag/src/lib/go-feature-flag-provider.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ export class GoFeatureFlagProvider implements Provider {
3838
constructor(options: GoFeatureFlagProviderOptions) {
3939
this.timeout = options.timeout || 0; // default is 0 = no timeout
4040
this.endpoint = options.endpoint;
41-
this.apiKey = options.apiKey;
41+
42+
// Add API key to the headers
43+
if (options.apiKey) {
44+
axios.defaults.headers.common['Authorization'] = `Bearer ${this.apiKey}`;
45+
}
4246
}
4347

4448
/**
@@ -170,19 +174,13 @@ export class GoFeatureFlagProvider implements Provider {
170174

171175
let apiResponseData: GoFeatureFlagProxyResponse<T>;
172176
try {
173-
const reqConfig: AxiosRequestConfig = {
177+
const response = await axios.post<GoFeatureFlagProxyResponse<T>>(endpointURL.toString(), request, {
174178
headers: {
175179
'Content-Type': 'application/json',
176180
Accept: 'application/json',
177181
},
178182
timeout: this.timeout,
179-
};
180-
181-
if (this.apiKey) {
182-
reqConfig.headers?.put('Authorization', `Bearer ${this.apiKey}`);
183-
}
184-
185-
const response = await axios.post<GoFeatureFlagProxyResponse<T>>(endpointURL.toString(), request, reqConfig);
183+
});
186184
apiResponseData = response.data;
187185
} catch (error) {
188186
if (axios.isAxiosError(error) && error.response?.status == 401) {

0 commit comments

Comments
 (0)