Home

Awesome

<p align="center"> <a href="http://kitura.io/"> <img src="https://raw.githubusercontent.com/Kitura/Kitura/master/Sources/Kitura/resources/kitura-bird.svg?sanitize=true" height="100" alt="Kitura"> </a> </p> <p align="center"> <a href="http://www.kitura.io/"> <img src="https://img.shields.io/badge/docs-kitura.io-1FBCE4.svg" alt="Docs"> </a> <a href="https://travis-ci.org/Kitura/Kitura-CredentialsGitHub"> <img src="https://travis-ci.org/Kitura/Kitura-CredentialsGitHub.svg?branch=master" alt="Build Status - Master"> </a> <img src="https://img.shields.io/badge/os-macOS-green.svg?style=flat" alt="macOS"> <img src="https://img.shields.io/badge/os-linux-green.svg?style=flat" alt="Linux"> <img src="https://img.shields.io/badge/license-Apache2-blue.svg?style=flat" alt="Apache 2"> <a href="http://swift-at-ibm-slack.mybluemix.net/"> <img src="http://swift-at-ibm-slack.mybluemix.net/badge.svg" alt="Slack Status"> </a> </p>

Kitura-CredentialsGitHub

Plugin for the Credentials framework that authenticate using GitHub

Summary

Plugin for Kitura-Credentials framework that authenticates using the GitHub web login with OAuth2 API.

Table of Contents

Swift version

The latest version of Kitura-CredentialsGitHub requires Swift 4.0 or newer. You can download this version of the Swift binaries by following this link. Compatibility with other Swift versions is not guaranteed.

Example of GitHub web login

This guide assumes basic knowledge of Kitura app routing.

First, set up the session middleware:

import KituraSession

router.all(middleware: Session(secret: "Very very secret..."))

Create an instance of CredentialsGitHub plugin and register it with Credentials framework:

import Credentials
import CredentialsGitHub

let credentials = Credentials()
let gitCredentials = CredentialsGitHub(clientId: gitClientId, clientSecret: gitClientSecret, callbackUrl: serverUrl + "/login/github/callback", userAgent: "my-kitura-app", options: ["scopes": ["user:email"]])
credentials.register(gitCredentials)

Where:

Next, specify where to redirect non-authenticated requests:

credentials.options["failureRedirect"] = "/login/github"

Connect credentials middleware to handle requests to a protected path on the server, such as /private:

router.all("/private", middleware: credentials)
router.get("/private/data", handler: { request, response, next in
  ...  
  next()
})

And call authenticate to login with GitHub and to handle the redirect (callback) from the GitHub login web page after a successful login:

router.get("/login/github", handler: credentials.authenticate(gitCredentials.name))

router.get("/login/github/callback", handler: credentials.authenticate(gitCredentials.name))

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE.