Last active
September 26, 2023 00:25
-
-
Save davisshaver/5a41a84de339f1612067119c9930998d to your computer and use it in GitHub Desktop.
WordPress/Coral Project Talk JWT integration
This file contains hidden or 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
<?php | |
use \Firebase\JWT\JWT as JWT_Wrapper; | |
/** | |
* Class wrapper for JWT tokens. | |
*/ | |
class JWT { | |
use Singleton; | |
private $key; | |
private $token; | |
public $encoded; | |
/** | |
* Setup actions. | |
*/ | |
public function setup() { | |
if ( ! is_user_logged_in() ) { | |
return; | |
} | |
add_filter( | |
'coral_auth_token', | |
function() { | |
return $this->get_JWT(); | |
} | |
); | |
} | |
private function get_JWT() { | |
$this->secret = getenv( 'TALK_JWT_SECRET' ); | |
$this->audience = getenv( 'TALK_JWT_AUD' ); | |
$this->prefix = getenv( 'TALK_JWT_USER_PREFIX' ); | |
$this->iss = getenv( 'TALK_JWT_ISS' ); | |
if ( ! empty ( $this->secret ) && ! empty ( $this->iss ) && ! empty( $this->audience ) ) { | |
$current_user = wp_get_current_user(); | |
$site = get_bloginfo(); | |
$this->token = array( | |
'jti' => uniqid(), | |
'iss' => $this->iss, | |
'email' => $current_user->user_email, | |
'aud' => $this->audience, | |
'sub' => $this->prefix . '-' .$current_user->user_email, | |
'exp' => time() + 60 * 60, | |
'un' => $current_user->user_login, | |
'id' => $current_user->ID, | |
'site' => sanitize_title_with_dashes( $site ) | |
); | |
$this->encoded = JWT_Wrapper::encode( | |
$this->token, | |
$this->secret | |
); | |
return $this->encoded; | |
} | |
} | |
} | |
add_action( 'after_setup_theme', [ '\JWT', 'instance' ] ); |
This file contains hidden or 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
const UsersService = require('../../../services/users'); | |
const UserModel = require('../../../models/user'); | |
module.exports = { | |
tokenUserNotFound: async ({jwt, token}) => { | |
const username = await UsersService.getInitialUsername( | |
jwt.un | |
); | |
let user = await UserModel.findOneAndUpdate({ | |
id: jwt.sub, | |
}, { | |
id: jwt.sub, | |
email: jwt.email, | |
username, | |
lowercaseUsername: username.toLowerCase(), | |
roles: [], | |
profiles: [{ | |
provider: 'WordPress', | |
id: `${jwt.site}-${jwt.id}`, | |
}, | |
{ | |
provider: "local", | |
id: jwt.email, | |
username: jwt.un | |
}], | |
provider: 'local', | |
status: { | |
username: { | |
status: 'SET', | |
history: [{ | |
status: 'SET', | |
}], | |
} | |
}, | |
metadata: { | |
displayName: jwt.un, | |
}, | |
}, { | |
setDefaultsOnInsert: true, | |
new: true, | |
upsert: true, | |
}); | |
if (!user) { | |
return null; | |
} | |
return user; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment