Logout implementation attempt #2
This commit is contained in:
parent
076249f088
commit
495f0b1600
5 changed files with 47 additions and 36 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { BootMixin } from '@loopback/boot';
|
import { BootMixin } from '@loopback/boot';
|
||||||
import { Application, ApplicationConfig, BindingScope } from '@loopback/core';
|
import { Application, ApplicationConfig } from '@loopback/core';
|
||||||
import { RepositoryMixin } from '@loopback/repository';
|
import { RepositoryMixin } from '@loopback/repository';
|
||||||
import { ServiceMixin } from '@loopback/service-proxy';
|
import { ServiceMixin } from '@loopback/service-proxy';
|
||||||
import { AuthenticationBindings, AuthenticationComponent } from '@loopback/authentication';
|
import { AuthenticationBindings, AuthenticationComponent } from '@loopback/authentication';
|
||||||
|
@ -13,6 +13,7 @@ import { AuthService, SzakdolgozatUserService } from './services';
|
||||||
import { GraphQLBindings, GraphQLServer } from '@loopback/graphql';
|
import { GraphQLBindings, GraphQLServer } from '@loopback/graphql';
|
||||||
import { UserResolver } from './graphql-resolvers/user-resolver';
|
import { UserResolver } from './graphql-resolvers/user-resolver';
|
||||||
import { SzakdolgozatBindings } from './bindings';
|
import { SzakdolgozatBindings } from './bindings';
|
||||||
|
import { SzakdolgozatAuthChecker } from './szakdolgozat-auth-checker';
|
||||||
|
|
||||||
export { ApplicationConfig };
|
export { ApplicationConfig };
|
||||||
|
|
||||||
|
@ -37,11 +38,10 @@ export class SzakdolgozatBackendApplication extends BootMixin(
|
||||||
this.get(TokenServiceBindings.TOKEN_SERVICE).then(tokenService => {
|
this.get(TokenServiceBindings.TOKEN_SERVICE).then(tokenService => {
|
||||||
this.bind(AuthenticationBindings.STRATEGY).to(new JWTAuthenticationStrategy(tokenService));
|
this.bind(AuthenticationBindings.STRATEGY).to(new JWTAuthenticationStrategy(tokenService));
|
||||||
});
|
});
|
||||||
|
this.bind(GraphQLBindings.GRAPHQL_AUTH_CHECKER).toProvider(SzakdolgozatAuthChecker);
|
||||||
|
|
||||||
this.service(SzakdolgozatUserService, UserServiceBindings.USER_SERVICE);
|
this.service(SzakdolgozatUserService, UserServiceBindings.USER_SERVICE);
|
||||||
this.service(AuthService, {defaultScope: BindingScope.REQUEST, key: SzakdolgozatBindings.AUTH_SERVICE});
|
this.service(AuthService, SzakdolgozatBindings.AUTH_SERVICE);
|
||||||
this.get(SzakdolgozatBindings.AUTH_SERVICE).then(service => this.bind(GraphQLBindings.GRAPHQL_AUTH_CHECKER)
|
|
||||||
.to((resolverData, roles) => service.authUser(resolverData, roles)));
|
|
||||||
|
|
||||||
this.projectRoot = __dirname;
|
this.projectRoot = __dirname;
|
||||||
this.bootOptions = {
|
this.bootOptions = {
|
||||||
|
|
|
@ -3,5 +3,5 @@ import { AuthService } from './services';
|
||||||
|
|
||||||
export namespace SzakdolgozatBindings {
|
export namespace SzakdolgozatBindings {
|
||||||
export const AUTH_SERVICE = BindingKey.create<AuthService>('szakdolgozat.auth');
|
export const AUTH_SERVICE = BindingKey.create<AuthService>('szakdolgozat.auth');
|
||||||
//export const AUTH_TOKEN = BindingKey.create<string>('szakdolgozat.auth.token');
|
export const AUTH_TOKEN = BindingKey.create<string>('szakdolgozat.auth_token');
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ import { arg, authorized, GraphQLBindings, Int, mutation, query, resolver, Resol
|
||||||
import { User } from '../models';
|
import { User } from '../models';
|
||||||
import { repository } from '@loopback/repository';
|
import { repository } from '@loopback/repository';
|
||||||
import { UserRepository } from '../repositories';
|
import { UserRepository } from '../repositories';
|
||||||
import { inject } from '@loopback/core';
|
import { Context, inject } from '@loopback/core';
|
||||||
import { AuthService, SzakdolgozatUserService } from '../services';
|
import { SzakdolgozatUserService } from '../services';
|
||||||
import { TokenServiceBindings, UserServiceBindings } from '@loopback/authentication-jwt';
|
import { TokenServiceBindings, UserServiceBindings } from '@loopback/authentication-jwt';
|
||||||
import { TokenService } from '@loopback/authentication';
|
import { TokenService } from '@loopback/authentication';
|
||||||
import { SecurityBindings, UserProfile } from '@loopback/security';
|
import { SecurityBindings, UserProfile } from '@loopback/security';
|
||||||
|
@ -22,9 +22,8 @@ export class UserResolver {
|
||||||
@inject(GraphQLBindings.RESOLVER_DATA) private readonly resolverData: ResolverData,
|
@inject(GraphQLBindings.RESOLVER_DATA) private readonly resolverData: ResolverData,
|
||||||
@inject(TokenServiceBindings.TOKEN_SERVICE) public jwtService: TokenService,
|
@inject(TokenServiceBindings.TOKEN_SERVICE) public jwtService: TokenService,
|
||||||
@inject(SecurityBindings.USER, {optional: true}) public user: UserProfile,
|
@inject(SecurityBindings.USER, {optional: true}) public user: UserProfile,
|
||||||
@inject(SzakdolgozatBindings.AUTH_SERVICE) private authService: AuthService
|
@inject.context() private context: Context
|
||||||
) {
|
) {
|
||||||
console.log('Auth service', authService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@mutation(returns => User)
|
@mutation(returns => User)
|
||||||
|
@ -58,10 +57,11 @@ export class UserResolver {
|
||||||
|
|
||||||
@authorized()
|
@authorized()
|
||||||
@mutation(returns => Boolean)
|
@mutation(returns => Boolean)
|
||||||
async logout(): Promise<boolean> {
|
async logout(@inject(SzakdolgozatBindings.AUTH_TOKEN) token: string): Promise<boolean> {
|
||||||
console.log('Logout service: ', this.authService);
|
console.log('Logout service: ', token);
|
||||||
console.log('token:', this.authService.receivedToken); //TODO
|
console.log('Context: ', this.context?.name);
|
||||||
await this.jwtService.revokeToken?.(this.authService.receivedToken);
|
//console.log('token:', this.authService.receivedToken); //TODO
|
||||||
|
//await this.jwtService.revokeToken?.(this.authService.receivedToken);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,7 @@
|
||||||
import { AuthenticateFn, AuthenticationBindings, AuthenticationStrategy } from '@loopback/authentication';
|
|
||||||
import { ExpressContext, ResolverData } from '@loopback/graphql';
|
|
||||||
import { Getter, inject } from '@loopback/core';
|
|
||||||
import { JWTAuthenticationStrategy } from '@loopback/authentication-jwt';
|
|
||||||
|
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
receivedToken: string;
|
receivedToken: string;
|
||||||
|
|
||||||
constructor(@inject(AuthenticationBindings.AUTH_ACTION) private authenticate: AuthenticateFn,
|
constructor() {
|
||||||
@inject.getter(AuthenticationBindings.STRATEGY)
|
console.log('new auth service');
|
||||||
readonly getStrategies: Getter<AuthenticationStrategy | AuthenticationStrategy[] | undefined>) {
|
|
||||||
console.log('Created auth service', new Error().stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
async authUser(resolverData: ResolverData, roles: string[]) {
|
|
||||||
const context = (<ExpressContext> resolverData.context);
|
|
||||||
const res = await this.authenticate(context.req);
|
|
||||||
const strat = <JWTAuthenticationStrategy> await this.getStrategies();
|
|
||||||
// Itt már biztosan van érvényes token
|
|
||||||
console.log('This: ', this);
|
|
||||||
this.receivedToken = strat.extractCredentials(context.req);
|
|
||||||
console.log('This: ', this);
|
|
||||||
console.log('Strat: ', strat);
|
|
||||||
console.log('Creds: ', strat.extractCredentials(context.req));
|
|
||||||
console.log('Res: ', (<any> res).id);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
32
backend/src/szakdolgozat-auth-checker.ts
Normal file
32
backend/src/szakdolgozat-auth-checker.ts
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import { AuthenticateFn, AuthenticationBindings, AuthenticationStrategy } from '@loopback/authentication';
|
||||||
|
import { AuthChecker, ExpressContext, ResolverData } from '@loopback/graphql';
|
||||||
|
import { Context, Getter, inject, Provider, ValueOrPromise } from '@loopback/core';
|
||||||
|
import { JWTAuthenticationStrategy } from '@loopback/authentication-jwt';
|
||||||
|
import { SzakdolgozatBindings } from './bindings';
|
||||||
|
import { AuthService } from './services';
|
||||||
|
|
||||||
|
export class SzakdolgozatAuthChecker implements Provider<AuthChecker> {
|
||||||
|
constructor(@inject(AuthenticationBindings.AUTH_ACTION) private authenticate: AuthenticateFn,
|
||||||
|
@inject.getter(AuthenticationBindings.STRATEGY)
|
||||||
|
readonly getStrategies: Getter<AuthenticationStrategy | AuthenticationStrategy[] | undefined>,
|
||||||
|
@inject(SzakdolgozatBindings.AUTH_SERVICE) private authService: AuthService,
|
||||||
|
@inject.context() private context: Context) {
|
||||||
|
console.log('new auth checker');
|
||||||
|
}
|
||||||
|
|
||||||
|
value(): ValueOrPromise<AuthChecker> {
|
||||||
|
return this.authUser.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
async authUser(resolverData: ResolverData, roles: string[]) {
|
||||||
|
const context = (<ExpressContext> resolverData.context);
|
||||||
|
const res = await this.authenticate(context.req);
|
||||||
|
const strat = <JWTAuthenticationStrategy> await this.getStrategies();
|
||||||
|
// Itt már biztosan van érvényes token
|
||||||
|
console.log('Context: ', this.context.name);
|
||||||
|
this.context.bind(SzakdolgozatBindings.AUTH_TOKEN).to(strat.extractCredentials(context.req));
|
||||||
|
console.log('This: ', this.authService);
|
||||||
|
console.log('Res: ', (<any> res).id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue