2016-04-03 17:59:27 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2016-04-05 20:22:13 +00:00
|
|
|
"crypto/md5"
|
2016-04-03 17:59:27 +00:00
|
|
|
"database/sql"
|
2016-04-05 20:22:13 +00:00
|
|
|
"fmt"
|
2016-04-03 17:59:27 +00:00
|
|
|
|
|
|
|
"github.com/osuripple/api/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetTokenFull retrieves an user ID and their token privileges knowing their API token.
|
|
|
|
func GetTokenFull(token string, db *sql.DB) (common.Token, bool) {
|
|
|
|
var uid int
|
|
|
|
var privs int
|
2016-04-05 20:22:13 +00:00
|
|
|
err := db.QueryRow("SELECT user, privileges FROM tokens WHERE token = ? LIMIT 1", fmt.Sprintf("%x", md5.Sum([]byte(token)))).Scan(&uid, &privs)
|
2016-04-03 17:59:27 +00:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
return common.Token{}, false
|
|
|
|
case err != nil:
|
|
|
|
panic(err)
|
|
|
|
default:
|
|
|
|
return common.Token{
|
|
|
|
Value: token,
|
|
|
|
UserID: uid,
|
|
|
|
Privileges: common.Privileges(privs),
|
|
|
|
}, true
|
|
|
|
}
|
|
|
|
}
|