WIP: authorize routes
This commit is contained in:
parent
a6e3c042fe
commit
5ba8859256
|
@ -83,6 +83,7 @@ func main() {
|
|||
log.Fatal("connection error", err)
|
||||
return
|
||||
}
|
||||
_ = store.SetMaster(*secret)
|
||||
defer store.Close()
|
||||
|
||||
bind := *addr + ":" + *port
|
||||
|
|
|
@ -18,6 +18,7 @@ type Authorization struct {
|
|||
}
|
||||
|
||||
type Store interface {
|
||||
SetMaster(secret string) error
|
||||
Add(auth *Authorization) error
|
||||
Set(auth *Authorization) error
|
||||
Get(id string) (*Authorization, error)
|
||||
|
|
|
@ -2,7 +2,9 @@ package authstore
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
@ -41,6 +43,33 @@ type PGStore struct {
|
|||
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 {
|
||||
ctx, done := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
|
||||
defer done()
|
||||
|
|
Loading…
Reference in New Issue