-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: store pending registration requests in db
- Loading branch information
Showing
6 changed files
with
181 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package domain | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/schema" | ||
"tailscale.com/tailcfg" | ||
"time" | ||
) | ||
|
||
type RegistrationRequest struct { | ||
MachineKey string `gorm:"primary_key;autoIncrement:false"` | ||
Key string `gorm:"type:varchar(64);unique_index"` | ||
Data RegistrationRequestData | ||
CreatedAt time.Time | ||
Authenticated bool | ||
Error string | ||
} | ||
|
||
func (r *RegistrationRequest) IsFinished() bool { | ||
return r.Authenticated || len(r.Error) != 0 | ||
} | ||
|
||
type RegistrationRequestData tailcfg.RegisterRequest | ||
|
||
func (hi *RegistrationRequestData) Scan(destination interface{}) error { | ||
switch value := destination.(type) { | ||
case []byte: | ||
return json.Unmarshal(value, hi) | ||
default: | ||
return fmt.Errorf("unexpected data type %T", destination) | ||
} | ||
} | ||
|
||
func (hi RegistrationRequestData) Value() (driver.Value, error) { | ||
bytes, err := json.Marshal(hi) | ||
return bytes, err | ||
} | ||
|
||
// GormDataType gorm common data type | ||
func (RegistrationRequestData) GormDataType() string { | ||
return "json" | ||
} | ||
|
||
// GormDBDataType gorm db data type | ||
func (RegistrationRequestData) GormDBDataType(db *gorm.DB, field *schema.Field) string { | ||
switch db.Dialector.Name() { | ||
case "sqlite": | ||
return "JSON" | ||
} | ||
return "" | ||
} | ||
|
||
func (r *repository) SaveRegistrationRequest(ctx context.Context, request *RegistrationRequest) error { | ||
tx := r.withContext(ctx).Save(request) | ||
|
||
if tx.Error != nil { | ||
return tx.Error | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *repository) GetRegistrationRequestByKey(ctx context.Context, key string) (*RegistrationRequest, error) { | ||
var m RegistrationRequest | ||
tx := r.withContext(ctx).First(&m, "key = ?", key) | ||
|
||
if errors.Is(tx.Error, gorm.ErrRecordNotFound) { | ||
return nil, nil | ||
} | ||
|
||
if tx.Error != nil { | ||
return nil, tx.Error | ||
} | ||
|
||
return &m, nil | ||
} | ||
|
||
func (r *repository) GetRegistrationRequestByMachineKey(ctx context.Context, key string) (*RegistrationRequest, error) { | ||
var m RegistrationRequest | ||
tx := r.withContext(ctx).First(&m, "machine_key = ?", key) | ||
|
||
if errors.Is(tx.Error, gorm.ErrRecordNotFound) { | ||
return nil, nil | ||
} | ||
|
||
if tx.Error != nil { | ||
return nil, tx.Error | ||
} | ||
|
||
return &m, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.