8000 feat: SSR by loks0n · Pull Request #5777 · appwrite/appwrite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: SSR #5777

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

Merged
merged 131 commits into from
Jan 17, 2024
Merged

feat: SSR #5777

merged 131 commits into from
Jan 17, 2024

Conversation

loks0n
Copy link
Member
@loks0n loks0n commented Jul 6, 2023

What does this PR do?

Session Secrets for API key authenticated clients

  • Adds the secret field to the Session response object when authenticated using API key, including the response of all existing Account session creation endpoints.
  • Adds the sessions scope, used for session creation endpoints. It is added available to guests by default and will be optional for API key.

Tokens

  • 🟢 New endpoint Create session in users service POST /users/:userId/session
  • 🟢 New endpoint Create token in users service POST /users/:userId/token
  • 🟢 New endpoint Create session [from token] in account service POST /account/sessions/token
  • 🔴 Removes and aliases PUT /account/sessions/phone to POST /account/sessions/token
  • 🔴 Removes and aliases PUT /account/sessions/magic-url to POST /account/sessions/token

Token for SSR OAuth

  • Appends URL parameters userId & secret from a generated token to the final OAuth2 redirect to allow SSR clients to exchange this for a session.

Test Plan

  • e2e tests
  • manual w/o sdk
  • manual w/ sdk

Related PRs and Issues

RFC

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

@loks0n loks0n changed the title feat: return encodedSecret in session body feat: SSR Jul 6, 2023
@loks0n loks0n self-assigned this Oct 11, 2023
@loks0n loks0n marked this pull request as ready for review October 11, 2023 16:00
App::post('/v1/users/:userId/sessions')
->desc('Create session')
->groups(['api', 'users'])
->label('event', 'users.[userId].sessions.create')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be users.[userId].sessions.[sessionId].create

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 1451 to 1453
if ($user !== false && !$user->isEmpty()) {
$user->setAttributes($user->getArrayCopy());
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the need for this ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

Comment on lines 1476 to 1502
$user->setAttributes([
'$id' => $userId,
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::user($userId)),
Permission::delete(Role::user($userId)),
],
'email' => null,
'emailVerification' => false,
'status' => true,
'password' => null,
'hash' => Auth::DEFAULT_ALGO,
'hashOptions' => Auth::DEFAULT_ALGO_OPTIONS,
'passwordUpdate' => null,
'registration' => DateTime::now(),
'reset' => false,
'prefs' => new \stdClass(),
'sessions' => null,
'tokens' => null,
'memberships' => null,
'search' => implode(' ', [$userId]),
'accessedAt' => DateTime::now(),
]);

$user->removeAttribute('$internalId');
Authorization::skip(fn () => $dbForProject->createDocument('users', $user));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a createSession endpoint right? I see this is performing 2 functions

  1. Creating a user
  2. Creating a session for the user

this goes against REST principles where each endpoint should handle only one responsibility unless absolutely necessary.

What is the need to create a user here when we already have the createUser endpoint?

This createSession endpoint should return 404 if the user does not exist

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it seems to violate REST principle, but it offers pontentially better DX, and Matej proposed this change justifying it as the existing behaviour for endpoints in account service.

Should we:

  1. Auto-create users for endpoints in account service and NOT users service?
  2. Auto-create users for endpoints in both account and users services?
  3. Change accounts service so we never auto-create users?

Comment on lines 1597 to 1619
$user->setAttributes([
'$id' => $userId,
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::user($userId)),
Permission::delete(Role::user($userId)),
],
'email' => null,
'emailVerification' => false,
'status' => true,
'password' => null,
'hash' => Auth::DEFAULT_ALGO,
'hashOptions' => Auth::DEFAULT_ALGO_OPTIONS,
'passwordUpdate' => null,
'registration' => DateTime::now(),
'reset' => false,
'prefs' => new \stdClass(),
'sessions' => null,
'tokens' => null,
'memberships' => null,
'search' => implode(' ', [$userId]),
'accessedAt' => DateTime::now(),
]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above. The endpoint should only perform one function.

App::post('/v1/users/:userId/tokens')
->desc('Create token')
->groups(['api', 'users'])
->label('event', 'users.[userId].tokens.create')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This event needs to be added to the events.php file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it should also be users.[userId].tokens.[tokenId].create

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment on lines 314 to 318
case 'token':
if (($auths['token'] ?? true) === false) {
throw new Exception(Exception::USER_AUTH_METHOD_UNSUPPORTED, 'Token authentication is disabled for this project');
}
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be added to the auth.php file in the config directory. Else it will break the updateAuthStatus endpoint

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also what is "token" based auth? How does it sit well in this screen?
Screenshot 2024-01-16 at 1 35 49 AM

I am to understand that tokens are created by emailPassword, phone, magic URL etc. So if we add a new check box for token here, what does it mean to disable token based auth?

does it mean, we also disable emailPassword, phone, magicURL etc? Because a token is generated by all those methods right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I think we should remove these. It doesn't fit in this screen.

The user can already control where a token is created with the existed toggles.
If we let them disable token auth it would break magic url and phone auth.

@@ -42,7 +42,7 @@
'name' => 'Phone',
'key' => 'phone',
'icon' => '/images/users/phone.png',
'docs' => 'https://appwrite.io/docs/client/account?sdk=web-default#accountCreatePhoneSession',
'docs' => 'https://appwrite.io/docs/references/cloud/client-web/account#accountCreatePhoneToken',
'enabled' => true,
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're missing a new array item for token

Comment on lines 4 to 6
'account' => [
'description' => 'Access to make actions on behalf of a user account',
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give me some reasons why this is not account.read and account.write

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was an existing hidden scope called account.
I wanted keep this name to reduce the scope of the changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

'account' => [
'description' => 'Access to make actions on behalf of a user account',
],
'sessions' => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly why is this not sessions.write ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

@loks0n loks0n requested a review from christyjacob4 January 17, 2024 12:10
@christyjacob4 christyjacob4 merged commit 7a17f10 into 1.5.x Jan 17, 2024
@stnguyen90 stnguyen90 deleted the feat-ssr branch April 25, 2024 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0