-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
authentication
filter under their own classes
- Loading branch information
Showing
5 changed files
with
67 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
jpro-auth/example/src/main/java/one/jpro/platform/auth/example/login/page/SignedInPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
jpro-auth/routing/src/main/java/one/jpro/platform/auth/routing/OAuth2Filter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package one.jpro.platform.auth.routing; | ||
|
||
import one.jpro.platform.auth.core.authentication.User; | ||
import one.jpro.platform.auth.core.oauth2.OAuth2AuthenticationProvider; | ||
import one.jpro.platform.auth.core.oauth2.OAuth2Credentials; | ||
import one.jpro.platform.routing.Filter; | ||
import one.jpro.platform.routing.Response; | ||
import one.jpro.platform.routing.Route; | ||
import simplefx.experimental.parts.FXFuture; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Defines a {@link Route} filter using OAuth2 authentication mechanism. | ||
* | ||
* @author Besmir Beqiri | ||
*/ | ||
public interface OAuth2Filter { | ||
|
||
/** | ||
* Creates {@link Route} filter from a given {@link OAuth2AuthenticationProvider}, | ||
* {@link OAuth2Credentials} and an operation a given user if the authentication | ||
* is successful. | ||
* | ||
* @param authProvider the OAuth2 authentication provider | ||
* @param credentials the OAuth2 credentials | ||
* @param userFunction operation on the given user argument | ||
* @param errorFunction operation on the given error argument | ||
* @return a {@link Filter} object | ||
*/ | ||
static Filter create(OAuth2AuthenticationProvider authProvider, | ||
OAuth2Credentials credentials, | ||
Function<User, FXFuture<Response>> userFunction, | ||
Function<Throwable, FXFuture<Response>> errorFunction) { | ||
Objects.requireNonNull(authProvider, "auth provider can not be null"); | ||
Objects.requireNonNull(credentials, "credentials can not be null"); | ||
Objects.requireNonNull(userFunction, "user function can not be null"); | ||
Objects.requireNonNull(errorFunction, "error function cannot be null"); | ||
|
||
return (route) -> (request) -> { | ||
if (request.path().equals(credentials.getRedirectUri())) { | ||
return FXFuture.fromJava(authProvider.authenticate(credentials)) | ||
.flatMap(userFunction::apply) | ||
.flatExceptionally(errorFunction::apply); | ||
} else { | ||
return route.apply(request); | ||
} | ||
}; | ||
} | ||
} |