-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add web.auth.UserInfo to map the returned user from a flow #27
Conversation
Given the above example, there is no way to also fetch the user's avatar image as shown in this piece of code: $auth= new SessionBased($flow, $sessions, function($client) use($storage) {
$res= $client->fetch('https://graph.microsoft.com/v1.0/me');
if ($res->status() >= 400) throw new AuthenticationException('Unexpected error', nameof($client));
// Cache user photo locally
$user= $res->value();
$picture= $client->fetch('https://graph.microsoft.com/v1.0/me/photo/$value');
if (200 === $picture->status()) {
$storage->store($user['id'], 'photo', $picture->header('Content-Type'), $picture->stream());
}
return $user;
}); |
After passsing the original authentication result as second argument to callbacks this can now be written as: $auth= new SessionBased($flow, $sessions, $flow->fetchUser('https://graph.microsoft.com/v1.0/me')
->map(function($user, $client) use($storage) {
$picture= $client->fetch('https://graph.microsoft.com/v1.0/me/photo/$value');
if (200 === $picture->status()) {
$storage->store($user['id'], 'photo', $picture->header('Content-Type'), $picture->stream());
}
return $user;
})
); |
Mapping the user info and being able to easily add debugging via peek would also be nice to have available for CAS authentication flows. This could be accomplished by adding a use web\auth\SessionBased:
use web\auth\cas\CasFlow;
use web\auth\oauth\OAuth2Flow;
// CAS uses the user info as returned
$flow= new CasFlow(/* ... */);
$userInfo= $flow->userInfo();
// OAuth fetches user info from a given endpoint
$flow= new OAuth2Flow(/* ... */);
$userInfo= $flow->fetchUser('https://graph.microsoft.com/v1.0/me');
// This call is the same in both cases
$auth= new SessionBased($flow, $sessions, $userInfo
->peek(Console::writeLine(...)) // Debugging
->map(fn($user) => ['ref' => $user['id'], 'name' => $user['displayName']])
); |
This pull request:
Example
The following shows how code can be rewritten when using this class:
Mapping the user
Often, the user object returned needs to be mapped to an app-specific structure, or cached in a local database. This is where the map() function comes into play:
Debugging
To debug the values inside a mapping pipeline, insert a peek() call: