Social Media Login
Plumier provided functionalities to easily secure your API and application using social media login such as Facebook, Google, GitHub and GitLab (Other provider will be added in the future). It included some security best practices out of the box, so you don't need to understand the security practice technically to implement social media login in Plumier.
caution
Source Code
#
Enable FunctionalitiesPlumier social media login is not enabled by default, to enable the functionalities use some provided Facility from @plumier/social-login
package.
Facility | Description |
---|---|
FacebookOAuthFacility | Provide Facebook specific social login functionalities |
GoogleOAuthFacility | Provide Google specific social login functionalities |
GitHubOAuthFacility | Provide GitHub specific social login functionalities |
GitLabOAuthFacility | Provide GitLab specific social login functionalities |
TwitterOAuthFacility | Provide Twitter specific social login functionalities |
To enable the social login functionality, register above facility on the Plumier application like below:
All OAuth provider facility (FacebookOAuthFacility
, GoogleOAuthFacility
, GitHubOAuthFacility
, GitLabOAuthFacility
) receive option parameter which required a client id and client secret. This value can be found in the appropriate social login console application.
#
Environment Variable for Default ConfigurationAll OAuth provider facility can be instantiated without parameters, It will automatically check for the environment variable for each Client ID and Client Secret. For example if the registration is like below
FacebookOAuthFacility
will search for environment variable PLUM_FACEBOOK_CLIENT_ID
and PLUM_FACEBOOK_CLIENT_SECRET
. If both not found an error will be thrown.
Facility | Client ID | Client Secret |
---|---|---|
FacebookOAuthFacility | PLUM_FACEBOOK_CLIENT_ID | PLUM_FACEBOOK_CLIENT_SECRET |
GoogleOAuthFacility | PLUM_GOOGLE_CLIENT_ID | PLUM_GOOGLE_CLIENT_SECRET |
GitHubOAuthFacility | PLUM_GITHUB_CLIENT_ID | PLUM_GITHUB_CLIENT_SECRET |
GitLabOAuthFacility | PLUM_GITLAB_CLIENT_ID | PLUM_GITLAB_CLIENT_SECRET |
TwitterOAuthFacility | PLUM_TWITTER_CLIENT_ID | PLUM_TWITTER_CLIENT_SECRET |
#
Redirect URI HandlerPlumier provided @redirectUri()
decorator to easily mark action as a social media redirect uri. To create an action as a redirect uri handler simply mark the action with the decorator like below.
Above is a common Plumier controller, no special configuration added except the @redirectUri()
. Above controller is a general redirect uri handler, its mean that it will handle all provider's redirect uris registered in the Plumier application into single method's controller.
Controller above handles GET /auth/callback
route, you can change this easily to match your redirect uri registered in the OAuth application provider.
#
Parameter BindingPlumier provided several parameter binding to bind social media login data into callback uri methods.
Decorator | Type | Description |
---|---|---|
@bind.oAuthUser() | OAuthUser | Bind generalized social media login user of type OAuthUser |
@bind.oAuthProfile() | any | Bind the raw social media user profile |
@bind.oAuthToken() | any | Bind social media auth token |
To use it simply apply it in the method's parameter like below
#
Separate Redirect UrisIf you have different redirect uri registered on your OAuth application, you can create separate redirect uri for specific provider, while keep using the general redirect uri:
If you provide a specific redirect uri handler, the general redirect uri will not be called.
#
OAuth UserOAuth user is the current social media user. If you notice all example above, all controller has parameter decorate with @bind.oAuthUser()
decorator.
provider
is the OAuth provider name calling the redirect uriid
is the user id on OAuth provider. Note that this field unique toprovider
fieldname
is the complete name of the current social media login userfirstName
is the first name of the current social media login userlastName
is the last name of the current social media login userprofilePicture
is the profile picture or avatar of the current social media login useremail
,gender
,dateOfBirth
is optional, its based on your OAuth application accessraw
is the raw value of the social media profile result.
#
Showing The Login PagePlumier provided endpoints that will be redirected to the social media login page. It generate the required parameter including the csrf token and then redirect the request to the generated url.
Endpoint | Served By | Description |
---|---|---|
GET /auth/facebook/login | FacebookOAuthFacility | Redirect to Facebook login page |
GET /auth/google/login | GoogleOAuthFacility | Redirect to Google login page |
GET /auth/github/login | GitHubOAuthFacility | Redirect to GitHub login page |
GET /auth/gitlab/login | GitLabOAuthFacility | Redirect to GitLab login page |
GET /auth/tiwtter/login | TwitterOAuthFacility | Redirect to Twitter login page |
Above endpoint can be change accordingly see customization section for more detail.
#
Social Media Login ButtonUse the provided endpoints above to create the social media login button, by showing a dialog window popup or redirect the user to the Facebook login screen.
Code above will redirect the user to the Facebook login screen.
Or you can show a popup dialog window like below:
When user click the link or button above, user will redirected to the Facebook login page properly.
#
Sending Message Back To Main WindowWhen social medial login process done using a popup dialog window, it needs a way to signal the main window that the login process successful or not.
Plumier provided an ActionResult returned html that will automatically post message to main window that open the login dialog. response.postMessage()
.
info
By default response.postMessage
will send message only to the same origin, you can change this behavior by providing the origin on second parameter.
Redirect uri handler above will send the { status: "Success", token }
message to the main window using post message, main window need to listen to the message
event like below:
Best Practice
Its required to check the ev.origin
of the message event because any window possible to send message to the main window.
#
CustomizationPlumier social login functionalities can be customize to match your need.
#
Custom Login EndpointLogin endpoint served by OAuth provider facility can be customized accordingly from the facility
#
Add Extra Parameter To Login EndpointIts possible to customize the generated login endpoint by providing the parameter. For example the default scope of google endpoint used is: https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
. You can override this by providing the scope on the login endpoint:
#
Add Extra Parameter on Profile EndpointOAuth provider facility used minimum configuration parameter to access the user profile endpoint. For example for Facebook provider the field params use is id,name,first_name,last_name,picture.type(large)
. You can override this by providing the parameters in the FacebookOAuthFacility
(other provider also can be configured) like below: