GithubHelp home page GithubHelp logo

mman / kitura-credentialshttp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kitura/kitura-credentialshttp

0.0 3.0 0.0 321 KB

A plugin for the Kitura-Credentials framework that authenticates using HTTP Basic and Digest authentication

License: Apache License 2.0

Swift 100.00%

kitura-credentialshttp's Introduction

Kitura

Docs Build Status - Master macOS Linux Apache 2 Slack Status

Kitura-CredentialsHTTP

A plugin for the Kitura-Credentials framework that authenticates using HTTP Basic and Digest authentication.

Summary

A plugin for Kitura-Credentials framework that authenticates using HTTP Basic and Digest authentication.

Our implementation of Digest authentication doesn't remember nonce values it generated, and doesn't check received request's nonce and nc. It uses MD5 algorithm, and the quality of protection (qop) is 'auth'.

Table of Contents

Swift version

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

API

Basic authentication

To create an instance of CredentialsHTTPBasic plugin, a VerifyPassword function and an optional realm should be passed to the constructor:

public init (verifyPassword: @escaping VerifyPassword, realm: String?=nil)

verifyPassword is a function of type:

/// Type alias for the callback that verifies the userId and password.
/// If the authentication pair verifies, then a user profile is returned.
public typealias VerifyPassword = (userId: String, password: String, callback: @escaping (UserProfile?) -> Void) -> Void

Digest authentication

CredentialsHTTPDigest initialization is similar to CredentialsHTTPBasic. In addition, an optional opaque value can be passed to the constructor.

Example

This example shows how to use this plugin to authenticate requests with HTTP Basic authentication. HTTP Digest authentication is similar.

First create an instance of Credentials and an instance of CredentialsHTTPBasic plugin, supplying a verifyPassword function:

import Credentials
import CredentialsHTTP

let credentials = Credentials()
let users = ["John" : "12345", "Mary" : "qwerasdf"]
let basicCredentials = CredentialsHTTPBasic(verifyPassword: { userId, password, callback in
    if let storedPassword = users[userId], storedPassword == password {
        callback(UserProfile(id: userId, displayName: userId, provider: "HTTPBasic"))
    } else {
        callback(nil)
    }
})

Now register the plugin:

credentials.register(plugin: basicCredentials)

Connect credentials middleware to profile requests:

router.all("/profile", middleware: credentials)

If the authentication is successful, request.userProfile will contain user profile information:

router.get("/profile", handler:
    { request, response, next in
      ...
      let profile = request.userProfile
      let userId = profile.id
      let userName = profile.displayName
      ...
      next()
})

Troubleshooting

Seeing error ld: library not found for -lCHttpParser for architecture x86_64 on build?

To solve this, go to your Xcode build settings and add $SRCROOT/.build/debug to the Library Search Paths for the CredentialsHTTP targets.

License

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

kitura-credentialshttp's People

Contributors

andrew-lees11 avatar bdhernand avatar carlbrown avatar dfirsht avatar gmosx avatar gtaban avatar ianpartridge avatar irar2 avatar jarrodparkes avatar kyemaloy97 avatar mman avatar na-gupta avatar quanvo87 avatar rolivieri avatar shmuelk avatar vadimeisenbergibm avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.