Code Monkey home page Code Monkey logo

gowebauth's Introduction

CircleCI GoDoc Go Report Card Coverage Status

GO Web Auth

Go library for providing middleware for HTTP auth schemes.

Examples

Single-user Basic Auth

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/clagraff/gowebauth"
	"github.com/nbari/violetear"
)

// route writes the requested URL to the HTTP response.
func route(w http.ResponseWriter, r *http.Request) {
	response := []byte(fmt.Sprintf("Successfully hit: %s", r.URL.String()))

	w.Write(response)
}

// main will setup a new HTTP server using gowebauth for authentication.
func main() {
	// Use whatever infrastructure you want. We use violetear only as an example.
	router := violetear.New()

	// Lets specifiy all valid users & create a realm for them
	admin := gowebauth.MakeUser("admin", "Password!")

	// Create middleware to be used by your router
	adminOnly := gowebauth.Handle(admin, route)

	router.Handle("/private", adminOnly, "GET")
	router.HandleFunc("/public", route, "GET")

	log.Panic(http.ListenAndServe(":8080", router))
}

Users and Realms

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/clagraff/gowebauth"
	"github.com/nbari/violetear"
)

// route writes the requested URL to the HTTP response.
func route(w http.ResponseWriter, r *http.Request) {
	response := []byte(fmt.Sprintf("Successfully hit: %s", r.URL.String()))

	w.Write(response)
}

// main will setup a new HTTP server using gowebauth for authentication.
func main() {
	// Use whatever infrastructure you want. We use violetear only as an example.
	router := violetear.New()

	// Lets specifiy all valid users & create a realm for them
	users := []gowebauth.User{
		gowebauth.MakeUser("admin", "Password!"),
		gowebauth.MakeUser("gon", "hunter123"),
		gowebauth.MakeUser("bennett", "qwop"),
	}
	realm, err := gowebauth.MakeRealm("Restricted Page", users)
	if err != nil {
	    panic(err)
    }

	// Wrap route to require authentication.
	privateRoute := gowebauth.Handle(realm, route)

	router.Handle("/private", privateRoute, "GET")
	router.HandleFunc("/public", route, "GET")

	log.Panic(http.ListenAndServe(":8080", router))
}

While any of the examples are running, you can try hitting a route using Curl:

$ curl -v -u admin:Password! localhost:8080/private
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
* Server auth using Basic with user 'admin'
> GET /private HTTP/1.1
> Host: localhost:8080
> Authorization: Basic YWRtaW46UGFzc3dvcmQh
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 26 Apr 2018 15:17:06 GMT
< Content-Length: 26
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact
Successfully hit: /private 

HTTP Digest Auth

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/clagraff/gowebauth"
	"github.com/nbari/violetear"
)

// route writes the requested URL to the HTTP response.
func route(w http.ResponseWriter, r *http.Request) {
	response := []byte(fmt.Sprintf("Successfully hit: %s", r.URL.String()))

	w.Write(response)
}

// main will setup a new HTTP server using gowebauth for authentication.
func main() {
	// Use whatever infrastructure you want. We use violetear only as an example.
	router := violetear.New()

	// Lets specifiy all valid users & create a realm for them
	users := []gowebauth.User{
		gowebauth.MakeUser("admin", "Password!"),
		gowebauth.MakeUser("gon", "hunter123"),
		gowebauth.MakeUser("bennett", "qwop"),
	}
	realm, err := gowebauth.MakeRealm("Restricted Page", users)
	if err != nil {
		panic(err)
	}
	digest := gowebauth.MakeDigest(realm, 15, 300*time.Second)

	// Wrap route to require authentication.
	privateRoute := gowebauth.Handle(digest, route)

	router.Handle("/private", privateRoute, "GET")
	router.HandleFunc("/public", route, "GET")

	log.Panic(http.ListenAndServe(":8080", router))
}

While this example is running, you can try hitting the route and formatting the digest-auth string.

Your exact bash commands will be different as the generated nonce will vary.

$ curl -v localhost:8080/private
curl -v localhost:8080/private
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /private HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 401 Unauthorized
< Www-Authenticate: Basic realm="Restricted Page" charset="utf-8" nonce="ad8fa7b5"
< Date: Wed, 23 May 2018 16:30:19 GMT
< Content-Length: 29
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact
malformed authorizationheader
$ echo -n "gon:Restricted Page:hunter123" | md5sum
f6def3aaf5ce8c891e4f74a95fcbf0a1  -
$ echo -n "GET:/private" | md5sum
fda2c070587e883e75df51c06f6c70d2  -
$ echo -n "f6def3aaf5ce8c891e4f74a95fcbf0a1:ad8fa7b5:fda2c070587e883e75df51c06f6c70d2" | md5sum
3a32bfdcd6ba445d611d297f085065c6  -
$ curl -v --header "Authorization: Digest username=\"gon\",realm=\"Restricted Page\",nonce=\"ad8fa7b5\",uri=\"/private\",qop=\"qop\",response=\"3a32bfdcd6ba445d611d297f085065c6\""  localhost:8080/private
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /private HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Authorization: Digest username="gon",realm="Restricted Page",nonce="ad8fa7b5",uri="/private",qop="qop",response="3a32bfdcd6ba445d611d297f085065c6"
>
< HTTP/1.1 200 OK
< Date: Wed, 23 May 2018 16:32:27 GMT
< Content-Length: 26
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact
Successfully hit: /private

License

MIT License

Copyright (c) 2018 Curtis La Graff

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

gowebauth's People

Contributors

clagraff avatar

Watchers

 avatar  avatar

gowebauth's Issues

IsAuthorized should return an identifier

As part of the Authorizer interface, the IsAuthorized(string) error function should return both an optional error, and the identifier for successful authentication strings.
A la IsAuthorized(token string) (Identity string, Error error)

Stale OneUseStore nonces never release memory

When using the OneUseStore, nonces can be generated without having any method of forcing their removal. This means an attacker could continue to make unauthenticated requests in order to generate more nonces which will never be removed, thus eating away at the servers resources.

Typo in readme

Under the nonce section, there is a typo.

$ echo -n "gon:Restricted Page:hunter123" | md5sum<Paste>

shouldnt have the <Paste> at the end.

Support additional nonce stores

The following nonce stores would be great to include:

  • Limitable-use nonce (3-use nonce, 2-use nonce, N-use nonce, etc)
  • Time-limited nonce (nonce valid for X seconds)

Do not export Nonce

Now that issue #18 has been merged, which un-exported a bunch of stuff from the packages, we no longer need the Nonce struct to be exported.

Remove backticks from docstrings

Please see the generated godoc docstring for MakeRealm. It can be found here.

Notice that there are backticks viewable in the docs. Remove these, as they should not have been there.

Epic Test

This is a test of creating an epic

Poor test coverage on FailureHandler

The Digest.FailureHandler function does not appear to be covered at all, when viewed with the coverage tool. This should be addressed as it is important to know that the function will work as expected.

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.