mirror of
https://gitlab.com/mobicoop/v3/service/auth.git
synced 2026-01-02 21:02:41 +00:00
refactor context sent to opa
This commit is contained in:
@@ -24,17 +24,25 @@ export class OpaDecisionMaker extends IMakeDecision {
|
||||
action: Action,
|
||||
context: Array<ContextItem>,
|
||||
): Promise<Authorization> {
|
||||
const { data } = await lastValueFrom(
|
||||
this._httpService.post<Decision>(
|
||||
this._configService.get<string>('OPA_URL') + domain + '/' + action,
|
||||
{
|
||||
input: {
|
||||
uuid,
|
||||
...context,
|
||||
},
|
||||
},
|
||||
),
|
||||
const reducedContext = context.reduce(
|
||||
(obj, item) => Object.assign(obj, { [item.name]: item.value }),
|
||||
{},
|
||||
);
|
||||
return new Authorization(data.result.allow);
|
||||
try {
|
||||
const { data } = await lastValueFrom(
|
||||
this._httpService.post<Decision>(
|
||||
this._configService.get<string>('OPA_URL') + domain + '/' + action,
|
||||
{
|
||||
input: {
|
||||
uuid,
|
||||
...reducedContext,
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
return new Authorization(data.result.allow);
|
||||
} catch (e) {
|
||||
return new Authorization(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,9 @@ const mockHttpService = {
|
||||
},
|
||||
},
|
||||
});
|
||||
})
|
||||
.mockImplementationOnce(() => {
|
||||
throw new Error();
|
||||
}),
|
||||
};
|
||||
|
||||
@@ -84,5 +87,14 @@ describe('OpaDecisionMaker', () => {
|
||||
);
|
||||
expect(authorization.allow).toBeFalsy();
|
||||
});
|
||||
it('should return a falsy authorization when an error happens', async () => {
|
||||
const authorization = await opaDecisionMaker.decide(
|
||||
'bb281075-1b98-4456-89d6-c643d3044a91',
|
||||
Domain.user,
|
||||
Action.read,
|
||||
[],
|
||||
);
|
||||
expect(authorization.allow).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user