2018-07-08 13:29:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/hex"
|
|
|
|
"git.xdrm.io/example/aicra/db"
|
2018-10-07 09:23:51 +00:00
|
|
|
"git.xdrm.io/go/aicra/api"
|
2018-10-01 13:15:02 +00:00
|
|
|
"git.xdrm.io/go/aicra/driver"
|
2018-07-08 13:29:25 +00:00
|
|
|
e "git.xdrm.io/go/aicra/err"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-10-01 13:15:02 +00:00
|
|
|
func main() {}
|
|
|
|
|
|
|
|
type TokenController int
|
|
|
|
|
|
|
|
func Export() driver.Controller { return new(TokenController) }
|
|
|
|
|
2018-07-08 13:29:25 +00:00
|
|
|
// Builds an access token from credentials
|
2018-10-07 09:23:51 +00:00
|
|
|
func (tctl TokenController) Post(d api.Arguments) api.Response {
|
2018-09-28 14:06:20 +00:00
|
|
|
|
2018-10-07 09:23:51 +00:00
|
|
|
r := api.NewResponse()
|
2018-07-08 13:29:25 +00:00
|
|
|
|
|
|
|
/* (1) Init redis connection */
|
|
|
|
cli := db.Connect()
|
|
|
|
if cli == nil {
|
|
|
|
r.Err = e.Failure
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Extract api input */
|
2018-10-07 09:23:51 +00:00
|
|
|
role, err := d.GetString("role")
|
|
|
|
if err != nil {
|
2018-07-08 13:29:25 +00:00
|
|
|
r.Err = e.InvalidParam
|
2018-10-01 19:21:33 +00:00
|
|
|
r.Err.Put("url")
|
2018-10-07 09:23:51 +00:00
|
|
|
r.Err.Put(err.Error())
|
2018-07-08 13:29:25 +00:00
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Generate token */
|
|
|
|
hasher := sha512.New()
|
|
|
|
defer hasher.Reset()
|
|
|
|
hasher.Write([]byte(strconv.FormatInt(time.Now().Unix(), 5)))
|
|
|
|
token := hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
|
|
|
|
/* (4) Store */
|
|
|
|
if !cli.Set(db.TOKEN, token, role, time.Minute) {
|
|
|
|
r.Err = e.Failure
|
|
|
|
return *r
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Set("token", token)
|
|
|
|
r.Err = e.Success
|
|
|
|
return *r
|
|
|
|
}
|
2018-10-01 13:15:02 +00:00
|
|
|
|
2018-10-07 09:23:51 +00:00
|
|
|
func (tctl TokenController) Get(d api.Arguments) api.Response { return *api.NewResponse() }
|
|
|
|
func (tctl TokenController) Put(d api.Arguments) api.Response { return *api.NewResponse() }
|
|
|
|
func (tctl TokenController) Delete(d api.Arguments) api.Response { return *api.NewResponse() }
|