This project provides OAuth2 demo of the Perfect Authentication libraries and select OAuth2 provider drivers - Facebook, Google, GitHub.
Full documentation can be found at http://www.perfect.org/docs/OAuth2.html
This package builds with Swift Package Manager and is part of the Perfect project. It was written to be stand-alone and so does not require PerfectLib or any other components.
Ensure you have installed and activated the latest Swift 3.0 tool chain.
Download this repo, clone it, or import it using Perfect Assistant. Build, configure the system with your AppID's and secrets, and run.
git clone https://github.com/PerfectExamples/Perfect-Authentication-Demo
cd Perfect-Authentication-Demo
swift build
.build/debug/Perfect-Authentication-Demo
Alternatively, build the Xcode project with swift package generate-xcodeproj
.
If you choose to generate an Xcode Project, you MUST change to the executable target AND set the custom working directory to the project directory for its scheme in order for the web routes to work properly.
Each provider needs an "appid", also known as a "key", and a "secret". These are usually generated by the OAuth Host, such as Facebook, GitHub and Google developer consoles. These values, as well as an "endpointAfterAuth" and "redirectAfterAuth" value must be set for each provider you wish to use. If you leave any unset, the authentication will fail.
These settings are configured by removing the ".default" extension from the "ApplicationConfiguration.json.default" file in the "config" directory, then populating the values as needed.
The default file looks like this:
{
"facebookAppID": "---",
"facebookSecret": "---",
"facebookEndpointAfterAuth": "http://localhost:8181/auth/response/facebook",
"facebookRedirectAfterAuth":"http://localhost:8181/",
"githubKey": "---",
"githubSecret": "---",
"githubEndpointAfterAuth": "http://localhost:8181/auth/response/github",
"githubRedirectAfterAuth":"http://localhost:8181/",
"googleKey": "---",
"googleSecret": "---",
"googleEndpointAfterAuth": "http://localhost:8181/auth/response/google",
"googleRedirectAfterAuth":"http://localhost:8181/"
}
Replace the ---
values with those from your own accounts.
Once the demo has been configured, it can be run from the command line, or from Xcode.
It can then be accessed from a web browser at http://localhost:8181
.
The first page you will see will have the following:
SessionID: <<SessionID>>
Login Type:
UserID:
UserName:
Picture:
AccessToken:
These values however will be populated upon login with one of the providers via the buttons.
Logging out will terminate the session, and start a new session without any information gained from a login with a provider.
Note that this demo uses the "MemorySessions" driver, so terminating the demo and restarting it will delete all existing sessions.
For more information on the Perfect project, please visit perfect.org.