2019-11-19 17:07:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
gorm.Model
|
|
|
|
Username string `gorm:"unique;not null"`
|
2019-11-19 22:35:04 +00:00
|
|
|
CryptedPassword []byte `gorm:"not null" json:"-"`
|
2019-11-19 17:07:56 +00:00
|
|
|
Email string `gorm:"unique;not null"`
|
|
|
|
IsAdmin bool `gorm:"default:false"`
|
|
|
|
CanCreateHandlers bool `gorm:"default:false"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Token struct {
|
|
|
|
gorm.Model
|
|
|
|
Body string `gorm:"unique;not null"`
|
|
|
|
UserID uint
|
|
|
|
User User
|
|
|
|
ExpiresAt time.Time
|
|
|
|
}
|
|
|
|
|
2019-12-08 19:13:36 +00:00
|
|
|
type ExecutionLog struct {
|
|
|
|
gorm.Model
|
|
|
|
RunID string `gorm:"unique;not null"`
|
|
|
|
HandlerID uint
|
|
|
|
Data []byte `gorm:"not null"`
|
|
|
|
}
|
|
|
|
|
2019-11-19 17:07:56 +00:00
|
|
|
type Handler struct {
|
|
|
|
gorm.Model
|
2019-12-08 19:13:36 +00:00
|
|
|
Name string `gorm:"unique;not null"`
|
2019-12-13 04:55:20 +00:00
|
|
|
Path string
|
|
|
|
UserID uint `json:"-"`
|
|
|
|
User User `json:"-"`
|
2019-11-19 17:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t Token) ToCookie() *http.Cookie {
|
|
|
|
return &http.Cookie{
|
|
|
|
Name: "wasmcloud-token",
|
|
|
|
Value: t.Body,
|
|
|
|
Expires: t.ExpiresAt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeToken(u User) (Token, error) {
|
|
|
|
t := Token{
|
|
|
|
Body: uuid.New().String(),
|
|
|
|
UserID: u.ID,
|
|
|
|
ExpiresAt: time.Now().Add(*defaultTokenLifetime),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := db.Save(&t).Error
|
|
|
|
if err != nil {
|
|
|
|
return Token{}, fmt.Errorf("error when creating token: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|