loopback4-kratos
v1.0.1
Published
A simple Ory Kratos integration in loopback4 applications
Downloads
1
Maintainers
Readme
loopback4-kratos
A simple Ory Kratos integration in loopback4 applications.
Installation
Install KratosComponent using npm
;
$ [npm install | yarn add] loopback4-kratos
Basic Use
import {AuthenticationComponent} from '@loopback/authentication';
import {
KratosComponentBindings,
KratosComponent,
} from 'loopback4-kratos';
// ...
export class MyApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options: ApplicationConfig = {}) {
// ...
this.component(AuthenticationComponent);
this.component(KratosComponent);
this.bind(KratosComponentBindings.CONFIG).to({
baseUrl: 'http://kratos_url'
});
// To register a custom user service
this.bind(KratosComponentBindings.USER_SERVICE.key).toClass(
MyUserService
);
// ...
}
// ...
}
It is therefore necessary to define a new user service:
import {UserProfile} from '@loopback/security';
import {
KratosUserService
} from 'loopback4-kratos';
import {Session} from '@ory/kratos-client';
// ...
export class MyUserService extends KratosUserService {
convertToUserProfile(response: Session): UserProfile {
const ans = super.convertToUserProfile(response);
// Implement your strategy ...
return ans;
}
// ...
}
After this, you can just use Kratos as authentication strategy across application.
import {authenticate} from '@loopback/authentication';
import {get} from '@loopback/rest';
// ...
export class YourController {
@get('/foo')
@authenticate('kratos')
foo() {
// this request is protected by kratos authentication
}
// ...
}