OAuth 2.0 is widely used in authentication and authorization that allows third-party applications to access resources that are hosted on other applications. OAuth 2.0 provides access to resources with some limitations to the client applications without sharing the actual credentials of the user. Here I will specify my own experience that how I implemented Oauth2 in our App. We implemented a few mini-programs in our app using a third-party mini-app container.
Let's understand more about mini-programs and mini-app containers.
Mini Programs:
Mini programs are mini-apps or we can say independent child apps with limited features that run inside a parent app or bigger app. Mini programs can perform a variety of functions that can enhance native app features like food ordering, ticket booking, banking, chat, E-commerce, and promo codes and can take your small business app to the next level of growth. It allows 3rd parties to implement new features and services on your native app and thus it can nurture your existing customers and access new ones to your business.
Mini APP Container:
the mini-app container is a runtime system that can run a variety of mini-programs safely and make your native APP more efficient and flexible. The container can run on both Android and iOS platforms. App container delivers rich functionality and capabilities to the user using mini-programs without the need for republishing the native apps.
Authorization Flow:
Understand the Authorization Flow:
Now we will understand more about the working of OAuth 2.0 in real programming scenarios. The above flow diagram is showing how does a user login into a mini-program using its native app. So basically mini-program will access user account details that are hosted on the server of the native app. Initially, the user login to the native app and then launches a mini-program through the native app, then mini-program requests AuthCode from the native app. As soon as the native app gets the request from the mini-program it represents a prompt to the user asking if he wants to authorize this request. If the user approves the request, the native app makes an Auth Code request to the authorization server on behalf of the mini-program by sending its client id and client secret. The authorization server verifies the client and user info and after verification, it issues an Auth Code for a specified scope to the native app. The native app returns this auth code to the mini-program.
Now, mini-program directly invokes the authorization server by sending the received auth code. After verifying auth code, the authorization server generates an access token with an expiration time and returns it to the mini program. This access token can be passed in the user details API of the authorization server to fetch user information like user id, username, email, and contact number.
When the authorization server receives a request with an access token, it verifies the token and also checks if it has not been expired. If the token has been expired then the current request will be failed by saying the access token has been expired. In this case, the mini-program will need to refresh the token by sending a new request to the authorization server for getting a new access token. Then mini-program again sends this new token with user details API and after verification gets the details of the user.
Comments
Post a Comment