Skip to content

Commit

Permalink
Merge pull request #1287 from multiversx/SERVICES-2112-upgrade-native…
Browse files Browse the repository at this point in the history
…-auth-server

[SERVICES-2112] use impersonate-address from request headers
  • Loading branch information
claudiulataretu authored Feb 27, 2024
2 parents 70e4136 + 4f20c9e commit 9f50599
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/modules/auth/native.auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Logger } from 'winston';
@Injectable()
export class NativeAuthGuard implements CanActivate {
private readonly authServer: NativeAuthServer;
private impersonateAddress: string;

constructor(
private readonly apiConfigService: ApiConfigService,
Expand Down Expand Up @@ -57,6 +58,10 @@ export class NativeAuthGuard implements CanActivate {
const ctx = GqlExecutionContext.create(context);
const { req } = ctx.getContext();

if (req.headers !== undefined) {
this.impersonateAddress = req.headers['impersonate-address'];
}

const authorization: string = req.headers['authorization'];
const origin = req.headers['origin'];

Expand Down Expand Up @@ -89,6 +94,17 @@ export class NativeAuthGuard implements CanActivate {
req.auth = userInfo;
req.jwt = userInfo;

if (this.impersonateAddress) {
const admins = process.env.SECURITY_ADMINS.split(',');
if (admins.find((admin) => admin === userInfo.signerAddress)) {
req.res.set(
'X-Native-Auth-Address',
this.impersonateAddress,
);
req.auth.address = this.impersonateAddress;
}
}

return true;
} catch (error: any) {
this.logger.error(`${NativeAuthGuard.name}: ${error.message}`);
Expand Down

0 comments on commit 9f50599

Please sign in to comment.