Simple Express Authorization Code Grant Example for Nextcloud's Custom App Social Login Based on module oauth2-server.
git clonethis Repo;- make sure that mongodb service running in your
env; cdinto project root folder and runnodemon start;
Social Login app Makes possible create users and login via Telegram, OAuth or OpenID, after install this app on Nextcloud, we can config a Custom Oauth2 plugin this:
For authorization code grant mode,there are actually 3 kernel steps:
Authorize: get authorizationCode;Token: use authorizationCode to exchange accessToken;Authenticate: use accessToken to get 'classified' information.
I used oauth2-server module to build my authorization flow.
authorize-handler(node-oauth-server/lib/handlers/authorize-handler.js),
For authorize process, oauth2-server module uses so called in this part Social Login will POST a query to get authorization code, just like http://server_addresss/oauth/authorize?response_type=code&client_id=_nextcloud&redirect_uri=redirect_uri&scope=&state=HA-GOVXLJMQZB7NW3FH8UYCA50RS9IDK26T14PE, basic flow is:
getClient(): fromrequest.bodygetclient_idandclient_secret,then get a client object, you need to implement this function in your own models;getUser(): fromrequest.bodyorrequset.sessiongetuser_id,then get a user object, you need to implement this function in your own models;generateAuthorizationCode():use client object & user object generate one authorizationcode, module has done this for you;saveAuthorizationCode(): save authorization code, you need to implement this function in your own models;- oauth server return a response with code in it:
http://redirect_uri&code=your_code&state=some_state.
Step 2:Token
token-handler(node-oauth-server/lib/handlers/token-handler.js),
In this process, Social Login will POST a query with authorization code to get access_token. Url may looks like: http://server_addresss/oauth/token?code=your_code&state=HA-GOVXLJMQZB7NW3FH8UYCA50RS9IDK26T14PE, basic flow is:
getClient(): same as Authorize;handleGrantType(): as for authorization code grant type, this function use handler ofnode-oauth-server/lib/grant-types/authorization-code-grant-type.js;getAuthorizationCode(): use the authcode inrequest.bodyto get a authorizationcode object, you need to implement this function in your own models;validateRedirectUri(): validate whether redirect uri of requset and redirect uri of authcode got above is the same one, module has done this for you;revokeAuthorizationCode(): delete code has been used, you need to implement this function in your own models;saveToken(): save token to your storage, you need to implement this function in your own models;
updateSuccessResponse(): return a response with token to Social Login.
Step 3:Authenticate
authenticate-handler(node-oauth-server/lib/handlers/authenticate-handler.js),
After gets access_token, Social Login tries to GET user info with access_token, url may looks like: http://server_addresss/userinfo?token=your_token&state=HA-GOVXLJMQZB7NW3FH8UYCA50RS9IDK26T14PE, basic flow is:
getTokenFromRequest(): getaccess_tokentoken value from request;getAccessToken(): use token value get anaccess_tokenobject, you need to implement this function in your own models;validateAccessToken(): validate access token.
After all this steps, if everything works fine, we can login to Nextcloud based on own user information.
