OpenAPITools / OpenAPITools/openapi-generator
[typescript] Using RxJS with Inversify (Authentication questions)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
I've been using the typescript generator and came to a common scenario - user authentication using Bearer token.
I couldn't find any information about the usage of the generated code, but I figured my way through it.
container.ts
import 'reflect-metadata';
import { Container, BindingScopeEnum } from 'inversify';
import { ApiServiceBinder } from '@app/data-client';
import { SERVICES } from './services';
const container = new Container({ defaultScope: BindingScopeEnum.Singleton });
container.bind(SERVICES.UserService.token).to(SERVICES.UserService.implementation);
const apiBinder = new ApiServiceBinder(container);
apiBinder.bindServerConfigurationToURL('https://test.dev/');
apiBinder.bindAllApiServices();
export default container;
user-service.ts
@injectable()
export class UserService {
@inject(AbstractUserSessionApi) private userSessionApi: UserSessionApi;
@inject(AbstractUserSettingsApi) private userSettingsApi: UserSettingsApi;
public signIn(credentials: LoginCredentials['user']) {
return this.userSessionApi.login({ user: credentials }).pipe(
tap((token) => console.log(token))
switchMap(() => this.fetch())
);
}
private fetch() {
return this.userSettingsApi.details();
}
}
Pretty simple configuration for now. User sends username/password -> we get the token via this.userSessionApi.login and then fire a fetch request to get the user data this.userSettingsApi.details().
Question here is - how can I set the token on the fly? The this.userSettingsApi.details() method has this:
authMethod = config.authMethods["Bearer"]
if (authMethod) {
await authMethod.applySecurityAuthentication(requestContext);
}
Then the BearerAuthentication has:
public constructor(@inject(AuthApiKey) @named("Bearer") private apiKey: string) {}
So - rebinding the AuthApiKey symbol is the way I was onto, but no luck...
public signIn(credentials: LoginCredentials['user']) {
return this.userSessionApi.login({ user: credentials }).pipe(
tap(async (token) => {
const t = await token;
container.bind(AuthApiKey).toConstantValue(t.token).whenTargetNamed('Bearer');
console.warn(container);
}),
switchMap(() => this.fetch())
);
}
Also, there is an issue with the observable, returning a promise (?), but I'll create another issue for this one.
I'll be glad to submit a PR if necessary, with some guidance from you guys.
Thanks!
openapi-generator version
CLI: 5.1.1
Suggest a fix/enhancement
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with container.ts and user-service.ts, then trace the generated userSessionApi.login and userSettingsApi.details entry points into BearerAuthentication and its AuthApiKey binding. Verify how the token flows through the RxJS observable and determine what behavior is needed for the subsequent details request to use it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- api, authentication
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100