Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication with v1.0.0.alpha #285

Closed
saleweaver opened this issue Oct 13, 2024 · 2 comments
Closed

Authentication with v1.0.0.alpha #285

saleweaver opened this issue Oct 13, 2024 · 2 comments

Comments

@saleweaver
Copy link

saleweaver commented Oct 13, 2024

Hi,

I've created a angular wrapper based on version 0.2.9.alpha, which isn't working because of #263, RSocketWebsocketClient is not a constructor (likely it has more errors, couldn't test).

Link to the repo https://github.com/saleweaver/angular-rsocket (qwen coder made a mistake with async code, will be fixed...)

Now I was looking into upgrading to v1.0.0.alpha, but can't find a way to pass authentication to the client.
In version 0.0.29.alpha I could add tokens using WellKnownMimeTypes, but I can't find any documentation on v1 auth, and, looking through the code, can't find anything that seems related to auth.

I'm sure I'm missing something, can someone tell me what it is? Is there an option to add auth? If not, has there ever been a version 0.0.xxx where RSocketWebsocketClient could be instantiated in the browser - and if not, do you still accept pull requests for version 0.0.29?

Thanks a lot!

Here's some code how I added tokens to metadata to be read by spring rsocket.

// Adding token to setup payload

private getSetupPayload(): any {
    const metadataPairs: [WellKnownMimeType, Buffer][] = [];

    // If a token is provided, include it in the setup payload
    if (this.config!.token) {
      const token = this.resolveProvidedToken(this.config!.token);
      if (token) {
        metadataPairs.push([MESSAGE_RSOCKET_AUTHENTICATION, encodeBearerAuthMetadata(token)]);
      }
    }

    const compositeMetadata = encodeCompositeMetadata(metadataPairs);

    return {
      metadata: compositeMetadata,
    };
  }
// Adding token to request Metadata

  private getCompositeMetadata(token: string | Signal<string> | (() => (string | Signal<string>)) | undefined, route: string) {
    const resolvedToken = this.resolveProvidedToken(token);

    const metadataPairs: [WellKnownMimeType, Buffer][] = [
      [MESSAGE_RSOCKET_ROUTING, encodeRoute(route)],
    ];

    if (resolvedToken) {
      metadataPairs.push([
        MESSAGE_RSOCKET_AUTHENTICATION,
        encodeBearerAuthMetadata(resolvedToken),
      ]);
    }

    return encodeCompositeMetadata(metadataPairs);
  }

EDIT:

I've converted the RSocketWebsocketClient to typescript, now it works with v0.0.29

@saleweaver saleweaver closed this as not planned Won't fix, can't repro, duplicate, stale Oct 13, 2024
@viglucci
Copy link
Member

viglucci commented Oct 14, 2024

Hi @saleweaver -- I've added a basic example which shows how you can accomplish authentication at request time using composite metadata. Apologies this wasn't more straight forward / discoverable on your own.

This is pretty close to your example above, and I haven't tried testing this against spring so there may still be some nuance there. Notable differences are that I didn't add my metadata to the setup payload, but provided it on each request, I'll likely add an example of providing it in the setup payload soon as that seems more relevant.

Please see #286.

Note that in my example I used a Map rather than a tuple array for the metadata, it is possible that there is an issue with providing a tuple list to encodeCompositeMetadata.

Yours:

  private getCompositeMetadata(token: string | Signal<string> | (() => (string | Signal<string>)) | undefined, route: string) {
    const resolvedToken = this.resolveProvidedToken(token);

    const metadataPairs: [WellKnownMimeType, Buffer][] = [
      [MESSAGE_RSOCKET_ROUTING, encodeRoute(route)],
    ];

    if (resolvedToken) {
      metadataPairs.push([
        MESSAGE_RSOCKET_AUTHENTICATION,
        encodeBearerAuthMetadata(resolvedToken),
      ]);
    }

    return encodeCompositeMetadata(metadataPairs);
  }

vs.

Mine:

function makeMetadata(bearerToken?: string, route?: string) {
  const map = new Map<WellKnownMimeType, Buffer>();

  if (bearerToken) {
    map.set(
      MESSAGE_RSOCKET_AUTHENTICATION,
      encodeBearerAuthMetadata(Buffer.from(bearerToken))
    );
  }

  if (route) {
    const encodedRoute = encodeRoute(route);
    map.set(MESSAGE_RSOCKET_ROUTING, encodedRoute);
  }

  return encodeCompositeMetadata(map);
}

@saleweaver
Copy link
Author

Hey Kevin,

thanks for that, I'll give it a try. I didn't find the well known metadata enums in v1, but I likely didn't look close enough. Will have a look.

Thanks for the example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants