vapor-community / Imperial

Federated Authentication with OAuth providers
MIT License
153 stars 48 forks source link

Fix issue #43—Callback url registered incorrectly if using route groups #102

Open bgisme opened 1 month ago

bgisme commented 1 month ago

Attempt to fix issue #43—Callback url registered incorrectly if using route groups.

It adds an optional parameter to ServiceRegister called grouped: [PathComponent]

oAuth<OAuthProvider>(
        grouped: [PathComponent] = [],
        ...

Can be used with group routes like this...

let abComponents: [PathComponent] = ["a", "b"]
let abRoutes = routes.grouped(abComponents)
try abRoutes.oAuth(
    grouped: abComponents,
    from: Google.self,
    authenticate: "start",
    callback: "http://localhost:8080/a/b/oauth/google",
    scope: ["profile", "email"],
    redirect: "/done")

 // call to trigger... http://localhost:8080/a/b/start

Ungroued routes can omit the parameter. So existing usage should not change.

The parameter is passed down to FederatedServiceRouter configureRoutes(grouped:withAuthURL:authenticateCallback:on:) where the callback URL gets registered.

When breaking the callbackURL into path components, duplicate components at the head of the path are removed. So the grouped route is registered properly and matches the callback url.

Most of the work happens inside ImperialCore > Helpers > String + Tools.swift > pathComponents(grouped:)

Also added a few tests for different kinds of groups and callback urls.

Happy to add readme stuff if you like the fix.

0xTim commented 1 month ago

Just an FYI I'm going to be doing an Imperial code sprint next week to go through all the issues and PRs