Can specify certs now
This commit is contained in:
parent
d081639b88
commit
7a59ef6be3
2 changed files with 23 additions and 23 deletions
30
api/auth.go
30
api/auth.go
|
@ -6,14 +6,11 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
privKeyPath = "keys/app.rsa" // openssl genrsa -out app.rsa keysize
|
|
||||||
pubKeyPath = "keys/app.rsa.pub" // openssl rsa -in app.rsa -pubout > app.rsa.pub
|
|
||||||
tokenName = "AccessToken"
|
tokenName = "AccessToken"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,31 +23,28 @@ var (
|
||||||
errGenericError = errors.New("generic error")
|
errGenericError = errors.New("generic error")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func SetupCerts(p string) error {
|
||||||
var err error
|
var err error
|
||||||
dir, _ := filepath.Abs(filepath.Dir(privKeyPath))
|
|
||||||
fmt.Println(dir)
|
|
||||||
|
|
||||||
signKey, err = ioutil.ReadFile(privKeyPath)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Before exploding, check up one level...
|
log.Fatalf("Path error: ", err)
|
||||||
signKey, err = ioutil.ReadFile("../../" + privKeyPath)
|
}
|
||||||
|
|
||||||
|
// openssl genrsa -out app.rsa keysize
|
||||||
|
privKeyPath := fmt.Sprintf("%vapp.rsa", p)
|
||||||
|
signKey, err = ioutil.ReadFile(privKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error reading private key: ", err)
|
log.Fatalf("Error reading private key: ", err)
|
||||||
return
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// openssl rsa -in app.rsa -pubout > app.rsa.pub
|
||||||
|
pubKeyPath := fmt.Sprintf("%vapp.rsa.pub", p)
|
||||||
verifyKey, err = ioutil.ReadFile(pubKeyPath)
|
verifyKey, err = ioutil.ReadFile(pubKeyPath)
|
||||||
if err != nil {
|
|
||||||
// Before exploding, check up one level...
|
|
||||||
verifyKey, err = ioutil.ReadFile("../../" + pubKeyPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error reading public key: ", err)
|
log.Fatalf("Error reading public key: ", err)
|
||||||
return
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type authHandler func(http.ResponseWriter, *http.Request) error
|
type authHandler func(http.ResponseWriter, *http.Request) error
|
||||||
|
|
|
@ -24,8 +24,13 @@ func main() {
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.IntFlag{
|
cli.IntFlag{
|
||||||
Name: "port",
|
Name: "port",
|
||||||
Value: 8901,
|
|
||||||
Usage: "HTTP service port",
|
Usage: "HTTP service port",
|
||||||
|
Value: 8901,
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "keys",
|
||||||
|
Usage: "path to keys",
|
||||||
|
Value: "keys/",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: cmdServe,
|
Action: cmdServe,
|
||||||
|
@ -56,6 +61,7 @@ func cmdServe(c *cli.Context) {
|
||||||
httpAddr := fmt.Sprintf(":%v", c.Int("port"))
|
httpAddr := fmt.Sprintf(":%v", c.Int("port"))
|
||||||
|
|
||||||
datastore.Connect()
|
datastore.Connect()
|
||||||
|
api.SetupCerts(c.String("keys"))
|
||||||
|
|
||||||
m := http.NewServeMux()
|
m := http.NewServeMux()
|
||||||
m.Handle("/api/", http.StripPrefix("/api", api.Handler()))
|
m.Handle("/api/", http.StripPrefix("/api", api.Handler()))
|
||||||
|
|
Reference in a new issue