WIP: authorize routes

This commit is contained in:
AJ ONeal 2020-05-30 17:45:36 -06:00
parent a6e3c042fe
commit 5ba8859256
3 changed files with 31 additions and 0 deletions

View File

@ -83,6 +83,7 @@ func main() {
log.Fatal("connection error", err) log.Fatal("connection error", err)
return return
} }
_ = store.SetMaster(*secret)
defer store.Close() defer store.Close()
bind := *addr + ":" + *port bind := *addr + ":" + *port

View File

@ -18,6 +18,7 @@ type Authorization struct {
} }
type Store interface { type Store interface {
SetMaster(secret string) error
Add(auth *Authorization) error Add(auth *Authorization) error
Set(auth *Authorization) error Set(auth *Authorization) error
Get(id string) (*Authorization, error) Get(id string) (*Authorization, error)

View File

@ -2,7 +2,9 @@ package authstore
import ( import (
"context" "context"
"crypto/sha256"
"database/sql" "database/sql"
"encoding/base64"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"time" "time"
@ -41,6 +43,33 @@ type PGStore struct {
dbx *sqlx.DB dbx *sqlx.DB
} }
func (s *PGStore) SetMaster(secret string) error {
ctx, done := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
defer done()
pubBytes := sha256.Sum256([]byte(secret))
pub := base64.RawURLEncoding.EncodeToString(pubBytes[:])
pub = pub[:24]
auth := &Authorization{
Slug: "*",
SharedKey: secret,
MachinePPID: secret,
PublicKey: pub,
}
err := s.Add(auth)
query := `
UPDATE authorizations SET
machine_ppid=$1,
shared_key=$1,
public_key=$2,
deleted_at='1970-01-01 00:00:00'
WHERE slug = '*'
`
_, err = s.dbx.ExecContext(ctx, query, auth.MachinePPID, auth.PublicKey)
return err
}
func (s *PGStore) Add(auth *Authorization) error { func (s *PGStore) Add(auth *Authorization) error {
ctx, done := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second)) ctx, done := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
defer done() defer done()