-
Notifications
You must be signed in to change notification settings - Fork 63
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
Sign in Workflow with auth token or cookie redirect/payload #2346
Comments
Here is an example of ChatGPT using Auth0 for a web browser based mobile login flow: trim.4C77FAA8-3C2E-42B6-8933-6017B007025F.MOV |
Here is a ref to the Auth0 docs: OAuth2 has a similar flow: |
For our purposes, since we also want to support a https://developer.okta.com/docs/reference/api/oidc/#token So, when that endpoint is accessed with that grant_type, then we check for the refresh token in the request. If it is present and valid, then issue new JWT (access and refresh tokens). If not (here is where the cookie exchange comes into play), we could validate the cookie, and assuming it is valid, then we also return a new JWT (access and refresh tokens), but this time via a more secure redirect that only the app will be handling. Then from that point forward, the mobile app will attempt to refresh the tokens according to the normal flow mentioned above (to keep the user logged in for as long as they actively use the app). |
THanks @zdmc23 would the refresh_token be able to happen in the background, so the user would not experience the redirects? |
@corsacca Yes, with each API request, we could check the expiration date of the token, and just refresh it when it is getting close to expire. The user never notices anything different. Linking it to the usage is desirable, bc most often we do want the token to expire when they haven't been actively using the app/API (for security sake, so they haven't simply lost their phone or a hostile family member starts randomly checking apps, etc...) |
trim.66088B82-74DD-4E1A-BFC3-A011F411A837.MOV |
@corsacca I think we can make this work without many changes. It relies on the https://example.com/wp-login.php?redirect_to=/wp-json/jwt-auth/v1/token Upon successful login (whether standard username/password, MFA, SSO, whichever plugins are being used, so long as the standard WordPress Here is the code snippet to make it happen: All edits would be in: https://github.com/DiscipleTools/disciple-tools-theme/blob/develop/dt-core/libraries/wp-api-jwt-auth/public/class-jwt-auth-public.php /**
* Add the endpoints to the API
*/
public function add_api_routes() {
register_rest_route( $this->namespace, 'token', [
'methods' => 'GET',
'callback' => [ $this, 'exchange_cookie_for_jwt' ],
'permission_callback' => '__return_true',
] );
register_rest_route( $this->namespace, 'token', [
'methods' => 'POST',
'callback' => [ $this, 'generate_token' ],
'permission_callback' => '__return_true',
] );
register_rest_route( $this->namespace, 'token/refresh', [
'methods' => 'POST',
'callback' => [ $this, 'refresh_access_token' ],
'permission_callback' => '__return_true'
]
);
register_rest_route( $this->namespace, 'token/validate', [
'methods' => 'POST',
'callback' => [ $this, 'validate_token' ],
'permission_callback' => '__return_true',
] );
}
public function refresh_access_token(WP_REST_Request $request) {
if ( !$this->has_permission() ) {
return new WP_Error( __FUNCTION__, "You do not have permission for this", [ 'status' => 403 ] );
}
$this->validate_token( $request );
$token = $this->generate_token_static( 'dummy-email', 'dummy-password' );
//remove_filter( 'authenticate', [ $this, 'allow_programmatic_login' ], 10 );
if ( $token ) {
return [
'login_method' => DT_Login_Methods::MOBILE,
'jwt' => $token,
];
}
}
public function exchange_cookie_for_jwt(WP_REST_Request $request) {
//if ( !is_user_logged_in() ) {
if ( !wp_validate_auth_cookie() ) {
wp_redirect( '/wp-login.php' );
exit();
}
$token = $this->generate_token_static( 'dummy-email', 'dummy-password' );
wp_redirect( 'exp://127.0.0.1:8081/?token=' . $token );
//wp_redirect( 'discipletools://example.com/?token=' . $token );
//wp_redirect( 'dt://example.com/?token=' . $token );
exit();
}
|
A couple of notes:
if ( !wp_validate_auth_cookie() ) {
wp_redirect( '/wp-login.php' );
exit();
} Thx! |
Some other related things to eventually discuss, but not urgent:
{
"access_token" : "ey...onNtiw",
"token_type" : "Bearer",
"expires_in" : 3600,
"scope" : "openid email",
"refresh_token" : "ey...kk2VdY",
"id_token" : "ey...kpOurg"
} |
trim.C70F5CDB-0585-45B7-BB06-ABA6EBDF9AB0.MOV^ sorry, I realized the prev video did not show the actual WP login form flow (bc of a prev login attempt caching the cookie into the ios jar), but here is an example that also includes MFA with TOTP. It works nicely |
Hey @zdmc23, @kodinkat implemented some auto refresh strategy: #2548 I'm looking for what the mvp would be to get this working. Signing in from the website will be helpful in any app or external server. I need to double check how our SSO strategy could workflow works with this too. |
@corsacca Thx! What is the best way to download the theme for this particular commit hash? I can test it locally with my previous tester mobile app to try to confirm it |
Most efficient? Checkout the auth-redirect branch of the theme. |
No description provided.
The text was updated successfully, but these errors were encountered: