日本語 version はこちら
This repository is sample code for OAuth authorization flow(Authorization Code Flow with PKCE extension) By Expo AuthSession. This repository covers below topics.
- Expo AuthSession on iOS and Web
- Facebook OAuth
- Twitter OAuth 2.0 (Twitter OAuth2)
- Twitter OAuth 1-0a (Twitter 3-legged authorization)
- Demo
- Conclusion
- Glossary and Key Concept
- Implicit Flow
- Authorization Code Flow with PKCE extension
- Build Environmental
demo.mov
OAuth is all about enabling users to grant limited access to applications, and Browser-based OAuth handles these flow by browser and redirect-uri. Historically, OAuth is subject to privacy and security, so many practices have been considered by OSS.
- Current best practice is
Authorization Code Flow with PKCE extension(2022). - Process between Public Client and Authorization Server should be protected by
state parametersagainst CSRF attack. - Redirect URI should be exact match between registered on Authorization Server and Public/ Confidential client.
- Access tokens should not be exposed in the front channel. So
Implicit Flowis not recommended.
| Player | Descrioption | Example |
|---|---|---|
| Authorization Server | Authorization Server asks resource owner for authorization of access their resource to developer. | |
| Public Client | Public Client is User Interface which source code is readable. So Public Client must not have secret credential. Public Client request to Authorization Server for authorization of API access, and receive Authorization Response via Redirect Uri. |
Web SPA/ Mobile Apps |
| Confidential Client | Confidential Client is backend server registered on Authorization Server, and has such info as client-id, client secret, permitted redirect uri and so on. Confidential Client requests Authorization Server for Access Token. |
Your backend server |
| endpoints | Descrioption |
|---|---|
| Authorization Endpoint | Authorization Server asks user for authorizing to access their resource by developer and then return user's answer(Authorization Response) such as Authorization Code to Puclic Client by Redirect URI. |
| Token Endpoint | Confidential Client requests Authorization Server to exchange Authorization Code for Access Token with registered credential info. After verify request, Authorization Server return Access Token to Confidential client by Redirect URI |
| Pattern | Descrioption | Recommended |
|---|---|---|
| Implicit Flow | Flow such as Puclic Client requests Access Token to Authorization Server, and recieves it in front channel. |
No |
| Authorization Code Flow with PKCE extension | Public Client requests Authorization Server for short-lived Authorization Code in front channel. After Public Client passed it to Confidential Client, Confidential Client requests Authorization Server to exchange auth code for AccessToken in backend channel. |
Yes |
Access tokens should not be exposed in the front channel because of below risks.
- Interception of the Redirect URI
- Access Token Leak in Browser History
- Manipulation of Scripts
- Access Token Leak to Third Party Scripts
Authorization Code Flow with PKCE extensionhasConfidential ClientandToken EndpointwhichImplicit Flowdosen't have.Front Channel(Public Client)getAuthorization Codewhich couldn't access api by itself.Confidential client(your registered backend server)exchangesAuthorization CodeforAccess Tokenwith Secret Key and PKCE. That is how your service dosen't expose access token in front channel.
- PKCE is specification for verification that Both
Authorization EndpointandToken Endpointare requested by same process. Public Clientgenerates random strings ascodeVerifier, and make hash of it ascodeChallengeby Sha-2 algorithm.Public ClientrequestsAuthorization EndpointwithcodeChallenge. That is howAuthorization Serverrecognizes Who requested.Confidential ClientrequestsToken EndpointwithcodeVerifier. That is howAuthorization Serverrecognizes same process requested.- That is how oauth is protected against introspection of Authorization Response.
- The OAuth 2.0 Authorization Framework
- Proof Key for Code Exchange by OAuth Public Clients
- OAuth 2.0 for Browser-Based Apps
-
signup expo and get account name
https://expo.dev/ -
twitter dev > User authentication settings
https://developer.twitter.com/
Register Redirect URIshttps://auth.expo.io/@{Expo Account Name}/expo-authsession-authcodeflow,https://localhost:19006/twitterOAuth2,https://localhost:19006/
-
facebook dev > Facebook Login setting
https://developers.facebook.com/
Register Redirect URIshttps://auth.expo.io/@{Expo Account Name}/expo-authsession-authcodeflow,https://localhost:19006/
cp .env.sample .env
### you get credential info at Authorization Server(Twitter/ Facebook), and write them in .env
### clone source
git clone git@github.com:Minminzei/expo-authsession-authcodeflow.git
cd expo-authsession-authcodeflow
yarn
### docker
docker-compose up -d
docker-compose exec app bash
yarn server
### simulatar
yarn ios
yarn web

