2017-04-28 21:57:10 +00:00
|
|
|
package database
|
|
|
|
|
2017-04-29 02:47:24 +00:00
|
|
|
import (
|
2017-12-15 20:56:20 +00:00
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2017-04-29 02:47:24 +00:00
|
|
|
proto "git.xeserv.us/xena/route/proto"
|
|
|
|
"github.com/Xe/ln"
|
2017-12-15 20:56:20 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-04-29 02:47:24 +00:00
|
|
|
)
|
2017-04-28 21:57:10 +00:00
|
|
|
|
2017-12-15 20:56:20 +00:00
|
|
|
// Tokens is the set of API calls needed to manage Token resources.
|
|
|
|
//
|
|
|
|
// Database backends should implement this interface.
|
|
|
|
type Tokens interface {
|
|
|
|
io.Closer
|
|
|
|
|
|
|
|
Get(ctx context.Context, id string) (Token, error)
|
|
|
|
GetBody(ctx context.Context, body string) (Token, error)
|
|
|
|
GetAll(ctx context.Context, user string) ([]Token, error)
|
|
|
|
Put(ctx context.Context, t Token) (Token, error)
|
|
|
|
Delete(ctx context.Context, id string) (Token, error)
|
|
|
|
DeleteExpired(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
2017-04-28 21:57:10 +00:00
|
|
|
// Token is a single authorization token.
|
|
|
|
type Token struct {
|
|
|
|
ID string `storm:"id"`
|
|
|
|
Body string `storm:"unique"`
|
|
|
|
Owner string `storm:"index"`
|
|
|
|
Scopes []string
|
|
|
|
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
2017-12-15 20:56:20 +00:00
|
|
|
ExpiresAt time.Time `json:"expires_at"`
|
2017-04-28 21:57:10 +00:00
|
|
|
Active bool `json:"active"`
|
|
|
|
}
|
2017-04-29 02:47:24 +00:00
|
|
|
|
|
|
|
// F https://godoc.org/github.com/Xe/ln#F
|
|
|
|
func (t Token) F() ln.F {
|
2017-12-15 20:56:20 +00:00
|
|
|
f := ln.F{
|
|
|
|
"token-id": t.ID,
|
|
|
|
"token-owner": t.Owner,
|
|
|
|
"token-active": t.Active,
|
|
|
|
"token-created-at": t.CreatedAt.String(),
|
2017-04-29 02:47:24 +00:00
|
|
|
}
|
2017-12-15 20:56:20 +00:00
|
|
|
|
|
|
|
if !t.ExpiresAt.IsZero() {
|
|
|
|
f["token-expires-at"] = t.ExpiresAt.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
2017-04-29 02:47:24 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 20:56:20 +00:00
|
|
|
// AsProto converts this into a protobuf Token.
|
2017-04-29 02:47:24 +00:00
|
|
|
func (t Token) AsProto() *proto.Token {
|
|
|
|
return &proto.Token{
|
|
|
|
Id: t.ID,
|
|
|
|
Body: t.Body,
|
|
|
|
Scopes: t.Scopes,
|
|
|
|
Active: t.Active,
|
|
|
|
}
|
|
|
|
}
|