Skip to content

Commit

Permalink
custom auth
Browse files Browse the repository at this point in the history
  • Loading branch information
dezren39 committed Dec 21, 2023
1 parent 4f68c2b commit 343fa02
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 111 deletions.
10 changes: 0 additions & 10 deletions go.work

This file was deleted.

97 changes: 0 additions & 97 deletions go.work.sum

This file was deleted.

81 changes: 80 additions & 1 deletion sources/identity/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"time"

"github.com/nrednav/cuid2"

"github.com/charmbracelet/log"
"github.com/charmbracelet/ssh"
_ "github.com/tursodatabase/libsql-client-go/libsql"
Expand Down Expand Up @@ -43,7 +45,7 @@ func CheckPublicKey(ctx ssh.Context, key ssh.PublicKey) (*Result, error) {

log.Info("Checking public key", "publicKeyType", publicKeyType, "publicKeyString", publicKeyString)

stmt, err := db.Prepare("SELECT cu.charm_id, cu.name, cu.created_at, pk.created_at FROM charm_user cu JOIN public_key pk ON pk.user_id = cu.id WHERE pk.public_key = ?")
stmt, err := db.Prepare("SELECT cu.charm_id, COALESCE(cu.name, ''), cu.created_at, pk.created_at FROM charm_user cu JOIN public_key pk ON pk.user_id = cu.id WHERE pk.public_key = ?")
if err != nil {
return nil, fmt.Errorf("failed to prepare query: %w", err)
}
Expand All @@ -65,3 +67,80 @@ func CheckPublicKey(ctx ssh.Context, key ssh.PublicKey) (*Result, error) {

return &res, nil
}

func InsertPublicKey(user_id int64, key ssh.PublicKey) (int64, error) {
host := os.Getenv("TURSO_HOST")
if host == "" {
log.Fatal("TURSO_HOST is not set")
}
authToken := os.Getenv("TURSO_AUTH_TOKEN")
if authToken == "" {
log.Fatal("TURSO_AUTH_TOKEN is not set")
}
db, err := sql.Open("libsql", fmt.Sprintf("libsql://%s?authToken=%s", host, authToken))
if err != nil {
return -1, fmt.Errorf("failed to open db %s: %w", host, err)
}
defer db.Close()

publicKeyType := key.Type()
publicKeyString := base64.StdEncoding.EncodeToString(key.Marshal())

log.Info("Inserting public key", "publicKeyType", publicKeyType, "publicKeyString", publicKeyString)

stmt, err := db.Prepare("INSERT INTO public_key (user_id, public_key) VALUES (?, ?)")
if err != nil {
return -1, fmt.Errorf("failed to prepare query: %w", err)
}
defer stmt.Close()

result, err := stmt.Exec(user_id, publicKeyType+" "+publicKeyString)
if err != nil {
return -1, fmt.Errorf("failed to execute query: %w", err)
}

insertedId, err := result.LastInsertId()
if err != nil {
return -1, fmt.Errorf("failed to retrieve last insert id: %w", err)
}

return insertedId, nil
}

func InsertUser(ctx ssh.Context) (int64, error) {
host := os.Getenv("TURSO_HOST")
if host == "" {
log.Fatal("TURSO_HOST is not set")
}
authToken := os.Getenv("TURSO_AUTH_TOKEN")
if authToken == "" {
log.Fatal("TURSO_AUTH_TOKEN is not set")
}
db, err := sql.Open("libsql", fmt.Sprintf("libsql://%s?authToken=%s", host, authToken))
if err != nil {
return -1, fmt.Errorf("failed to open db %s: %w", host, err)
}
defer db.Close()

stmt, err := db.Prepare("INSERT INTO charm_user (charm_id, bio) VALUES (?, ?)")
if err != nil {
return -1, fmt.Errorf("failed to prepare insert statement: %w", err)
}
defer stmt.Close()

id := cuid2.Generate()

result, err := stmt.Exec(id, ctx.RemoteAddr().Network()+":"+ctx.RemoteAddr().String())
if err != nil {
return -1, fmt.Errorf("failed to execute insert: %w", err)
}

insertedId, err := result.LastInsertId()
if err != nil {
return -1, fmt.Errorf("failed to retrieve last insert id: %w", err)
}

log.Info("User inserted", "charm_id", id, "insertedId", insertedId)

return insertedId, nil
}
1 change: 1 addition & 0 deletions sources/identity/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/nrednav/cuid2 v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
Expand Down
2 changes: 2 additions & 0 deletions sources/identity/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/nrednav/cuid2 v1.0.0 h1:27dn1oGiG+23Wa8XJ2DHeMoMa18Zs9u1+UHI9IlcGKM=
github.com/nrednav/cuid2 v1.0.0/go.mod h1:pdRH5Zrjwnv8DZ74XvHR3jX+bzJNfQjwLQ3JgSI2EmI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down
20 changes: 17 additions & 3 deletions sources/identity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,30 @@ func main() {
logging.Middleware(),
func(h ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
result, err := auth.CheckPublicKey(s.Context(), s.PublicKey())
pkid, err := auth.CheckPublicKey(s.Context(), s.PublicKey())
switch {
case err == nil:
wish.Println(s, "Hey!", result)
wish.Println(s, "Hey!", pkid)

default:
publicKeyType := s.PublicKey().Type()
publicKeyData := base64.StdEncoding.EncodeToString(s.PublicKey().Marshal())
message := fmt.Sprintf("Hey, I don't know who you are! Error: %v, Result: %v, Public Key Type: %s, Public Key: %s", err, result, publicKeyType, publicKeyData)
message := fmt.Sprintf("Hey, I don't know who you are!\nError:\n%v\nPublic key:\n%s %s", err, publicKeyType, publicKeyData)
wish.Println(s, message)

uid, err := auth.InsertUser(s.Context())
if err != nil {
wish.Println(s, "Failed to insert user:", err)
} else {
wish.Println(s, "Inserted user id:", uid)

pkid, err := auth.InsertPublicKey(uid, s.PublicKey())
if err != nil {
wish.Println(s, "Failed to insert public key:", err)
} else {
wish.Println(s, "Inserted public key id: ", pkid, fmt.Sprintf("\n%s\n%s %s", "Inserted public key:", publicKeyType, publicKeyData))
}
}
}
h(s)
}
Expand Down

0 comments on commit 343fa02

Please sign in to comment.