Categorygithub.com/nanobox-io/golang-nanoauth
modulepackage
0.0.0-20200131131040-063a3fb69896
Repository: https://github.com/nanobox-io/golang-nanoauth.git
Documentation: pkg.go.dev

# README

Build Status GoDoc

golang-nanoauth

Nanoauth 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/nanobox-io/golang-nanoauth"
)

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

  fmt.Printf("Stopped serving! - %v\n",
  	nanoauth.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-NANOBOX-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, _ := nanoauth.Generate("logvac.nanopack.io")
	auth := nanoauth.Auth{
		Header:      "X-AUTH-TOKEN",
		Certificate: cert,
	}
	return auth.ListenAndServeTLS(config.ListenHttp, "secret", router, "/")
...

Contributing

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

# Functions

Generate is a helper function which generates a tls.Certificate for serving TLS requests.
ListenAndServe is a shortcut function which uses the default one.
ListenAndServeTLS is a shortcut function which uses the default one.
Load is a helper function to load a certificate and key from password protected files.

# Variables

DefaultAuth is the default Auth object.

# Structs

Auth is a structure containing listener information.