Merge pull request #27 from valitydev/fx/bouncer-context

Fix context
This commit is contained in:
Egor Cherniak 2022-04-06 14:48:14 +03:00 committed by GitHub
commit e8a1386088
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 21 deletions

View File

@ -9,6 +9,7 @@ import dev.vality.bouncer.ctx.ContextFragmentType;
import dev.vality.bouncer.decisions.Context;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.thrift.TDeserializer;
import org.apache.thrift.TSerializer;
import org.springframework.stereotype.Component;
@ -25,40 +26,33 @@ public class BouncerContextFactory {
@SneakyThrows
public Context buildContext(AnapiBouncerContext bouncerContext) {
var contextFragment = buildContextFragment(bouncerContext);
var serializer = new TSerializer();
var fragment = new dev.vality.bouncer.ctx.ContextFragment()
.setType(ContextFragmentType.v1_thrift_binary)
.setContent(serializer.serialize(contextFragment));
var userFragment = orgManagerService.getUserAuthContext(
var deserializer = new TDeserializer();
var contextFragment = orgManagerService.getUserAuthContext(
keycloakService.getAccessToken().getSubject());
var fragment = new ContextFragment();
deserializer.deserialize(fragment, contextFragment.getContent());
enrichContextFragment(bouncerContext, fragment);
contextFragment = new dev.vality.bouncer.ctx.ContextFragment()
.setType(ContextFragmentType.v1_thrift_binary)
.setContent(serializer.serialize(fragment));
var context = new Context();
context.putToFragments(bouncerProperties.getContextFragmentId(), fragment);
context.putToFragments("userorg", userFragment);
context.putToFragments(bouncerProperties.getContextFragmentId(), contextFragment);
return context;
}
private ContextFragment buildContextFragment(AnapiBouncerContext bouncerContext) {
private void enrichContextFragment(AnapiBouncerContext bouncerContext, ContextFragment fragment) {
var env = buildEnvironment();
var contextAnalyticsApi = buildAnapiContext(bouncerContext);
var contextReports = buildReportContext(bouncerContext);
return new ContextFragment()
.setAuth(buildAuth())
.setUser(buildUser())
fragment.setAuth(buildAuth())
.setEnv(env)
.setAnapi(contextAnalyticsApi)
.setReports(contextReports);
}
private User buildUser() {
var user = new User();
var token = keycloakService.getAccessToken();
return user
.setId(token.getSubject())
.setEmail(token.getEmail())
.setRealm(new Entity().setId(bouncerProperties.getRealm()));
}
private Auth buildAuth() {
var auth = new Auth();
var accessToken = keycloakService.getAccessToken();

View File

@ -16,7 +16,10 @@ import dev.vality.magista.InvoicePaymentFlowHold;
import dev.vality.magista.InvoicePaymentFlowInstant;
import dev.vality.magista.Payer;
import dev.vality.magista.*;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.apache.thrift.TDeserializer;
import org.apache.thrift.TSerializer;
import java.time.Instant;
import java.util.HashMap;
@ -170,8 +173,11 @@ public class MagistaUtil {
return DamselUtil.fillRequiredTBaseObject(new StatInvoiceTemplateResponse(), StatInvoiceTemplateResponse.class);
}
@SneakyThrows
public static ContextFragment createContextFragment() {
return DamselUtil.fillRequiredTBaseObject(new ContextFragment(), ContextFragment.class);
ContextFragment fragment = DamselUtil.fillRequiredTBaseObject(new ContextFragment(), ContextFragment.class);
fragment.setContent(new TSerializer().serialize(new dev.vality.bouncer.context.v1.ContextFragment()));
return fragment;
}
public static Judgement createJudgementAllowed() {