panva / openid-client

OAuth 2 / OpenID Connect Client API for JavaScript Runtimes
MIT License
1.83k stars 392 forks source link

new Strategy raises an error from Typescript transpiler when used in passport.use() #189

Closed Tenshock closed 5 years ago

Tenshock commented 5 years ago

Describe the bug When I instanciate a new Strategy object in Typescript and I use this object in passport.use(), it raises an error from the typescript transpiler on the new Strategy line as follows:

Argument of type 'Strategy<unknown, Client>' is not assignable to parameter of type 'Strategy'.
  Property 'authenticate' is missing in type 'Strategy<unknown, Client>' but required in type 'Strategy'.

To Reproduce Issuer and Client configuration:

import { AuthorizationParameters, Client, ClientMetadata, Issuer, Strategy, TokenSet } from 'openid-client'
import passport from 'passport'

class OidcRepositoryImpl {
    private static authorizationParameters: AuthorizationParameters
    private static client: Client
    private static openIDIssuer: Issuer<Client>

    private constructor(strategyName: string) {
        passport.use(
            strategyName,
            new Strategy({ client: OidcRepositoryImpl.client, params: OidcRepositoryImpl.authorizationParameters }, this.verify)
        )

        passport.serializeUser((user, done) => {
            done(null, user)
        })
        passport.deserializeUser((user, done) => {
            done(null, user)
        })
    }

    static async build(
        authorizationParameters: AuthorizationParameters,
        clientMetadata: ClientMetadata,
        issuerRealmUrl: string,
        strategyName: string
    ): Promise<OidcRepositoryImpl> {
        this.openIDIssuer = await Issuer.discover(issuerRealmUrl)
        this.client = new OidcRepositoryImpl.openIDIssuer.Client(clientMetadata)
        this.authorizationParameters = authorizationParameters

        return new OidcRepositoryImpl(strategyName)
    }
}

Steps to reproduce the behaviour:

  1. Write this code
  2. Try to build it with a transpiler, I use tsc
  3. See the same error

Expected behaviour I don't know what it should do but I was using the exact same code in openid-client@3.3.0 and since I updated the package to 3.7.1 I have this error. When I override the typescript definition of the openid-client Strategy with an authenticate attribute, it works as expected. I don't know if it is really a bug or if I misunderstood something.

I think it's not relevant to show the code that calls this class to instanciate the Strategy and the parameters, as the bug is not related to a particular behaviour after it is executed, as it does not compile.

Environment:

Additional context

panva commented 5 years ago

@Tenshock thank you for bringing this up. Apparently we missed this when bundling the types with 3.7.0. I just released 3.7.2 which fixes the issue 🤞

Tenshock commented 5 years ago

I'm always impressed by your quick answer and patches, thank you a lot.