Skip to content

mu-box/golang-microauth

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status GoDoc

golang-microauth

Microauth provides a uniform means of serving HTTP/S for golang projects securely. It allows the specification of a certificate (or generates one) as well as an auth token which is checked before the request is processed.

Quickstart

Import and serve

main.go

package main

import (
  "net/http"
  "fmt"
  "io"

  "github.com/mu-box/golang-microauth"
)

func main() {>
 http.HandleFunc("/", func(rw http.ResponseWriter, req *http.>Request) {
   io.WriteString(rw, "World, Hello!\n")
 })

  fmt.Printf("Stopped serving! - %v\n",
  	microauth.ListenAndServe("127.0.0.1:8081", "$ECRET", nil))
}

Test

$ curl localhost:8081 -i
# HTTP/1.1 401 Unauthorized
# Date: Thu, 09 Jun 2016 22:18:55 GMT
# Content-Length: 0
# Content-Type: text/plain; charset=utf-8

$ curl -H 'X-MICROBOX-TOKEN: $ECRET' localhost:8081 -i
# HTTP/1.1 200 OK
# Date: Thu, 09 Jun 2016 22:27:24 GMT
# Content-Length: 14
# Content-Type: text/plain; charset=utf-8
#
# World, hello!

Usage

Generate a cert and customize auth the token header

...
	cert, _ := microauth.Generate("logvac.microbox.cloud")
	auth := microauth.Auth{
		Header:      "X-AUTH-TOKEN",
		Certificate: cert,
	}
	return auth.ListenAndServeTLS(config.ListenHttp, "secret", router, "/")
...

Contributing

Contributions to the microbox-router project are welcome and encouraged. Contributions should follow the microbox Contribution Process & Guidelines.