47 lines
861 B
Go
47 lines
861 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"crypto/sha512"
|
||
|
"encoding/hex"
|
||
|
"git.xdrm.io/example/aicra/db"
|
||
|
e "git.xdrm.io/go/aicra/err"
|
||
|
i "git.xdrm.io/go/aicra/implement"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// Builds an access token from credentials
|
||
|
func Post(d i.Arguments, r *i.Response) i.Response {
|
||
|
|
||
|
/* (1) Init redis connection */
|
||
|
cli := db.Connect()
|
||
|
if cli == nil {
|
||
|
r.Err = e.Failure
|
||
|
return *r
|
||
|
}
|
||
|
|
||
|
/* (2) Extract api input */
|
||
|
role, ok := d["role"].(string)
|
||
|
if !ok {
|
||
|
r.Err = e.InvalidParam
|
||
|
r.Err.BindArgument("url")
|
||
|
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
|
||
|
}
|