2016-04-05 20:22:13 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
2017-06-17 16:11:10 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2016-04-05 20:22:13 +00:00
|
|
|
"fmt"
|
2017-07-01 19:37:25 +00:00
|
|
|
"strconv"
|
2016-12-01 22:16:36 +00:00
|
|
|
"time"
|
2016-04-05 20:22:13 +00:00
|
|
|
|
2016-08-15 11:37:03 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
|
2019-02-23 13:09:10 +00:00
|
|
|
"github.com/osuYozora/rippleapi/common"
|
|
|
|
"github.com/osuYozora/schiavolib"
|
2016-04-05 20:22:13 +00:00
|
|
|
)
|
|
|
|
|
2016-11-21 16:04:27 +00:00
|
|
|
// TokenSelfDeletePOST deletes the token the user is connecting with.
|
|
|
|
func TokenSelfDeletePOST(md common.MethodData) common.CodeMessager {
|
2016-06-14 08:03:53 +00:00
|
|
|
if md.ID() == 0 {
|
|
|
|
return common.SimpleResponse(400, "How should we delete your token if you haven't even given us one?!")
|
|
|
|
}
|
2017-06-17 16:11:10 +00:00
|
|
|
var err error
|
|
|
|
if md.IsBearer() {
|
|
|
|
_, err = md.DB.Exec("DELETE FROM osin_access WHERE access_token = ? LIMIT 1",
|
|
|
|
fmt.Sprintf("%x", sha256.Sum256([]byte(md.User.Value))))
|
|
|
|
} else {
|
|
|
|
_, err = md.DB.Exec("DELETE FROM tokens WHERE token = ? LIMIT 1",
|
|
|
|
fmt.Sprintf("%x", md5.Sum([]byte(md.User.Value))))
|
|
|
|
}
|
2016-06-14 08:03:53 +00:00
|
|
|
if err != nil {
|
|
|
|
md.Err(err)
|
|
|
|
return Err500
|
|
|
|
}
|
|
|
|
return common.SimpleResponse(200, "Bye!")
|
|
|
|
}
|
2016-06-14 09:32:11 +00:00
|
|
|
|
|
|
|
type token struct {
|
2016-12-01 22:16:36 +00:00
|
|
|
ID int `json:"id"`
|
|
|
|
Privileges uint64 `json:"privileges"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
LastUpdated common.UnixTimestamp `json:"last_updated"`
|
2016-06-14 09:32:11 +00:00
|
|
|
}
|
|
|
|
type tokenResponse struct {
|
|
|
|
common.ResponseBase
|
2016-07-10 18:53:06 +00:00
|
|
|
Tokens []token `json:"tokens"`
|
2016-06-14 09:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TokenGET retrieves a list listing all the user's public tokens.
|
|
|
|
func TokenGET(md common.MethodData) common.CodeMessager {
|
2017-07-01 19:37:25 +00:00
|
|
|
wc := common.Where("user = ? AND private = 0", strconv.Itoa(md.ID()))
|
|
|
|
if md.Query("id") != "" {
|
|
|
|
wc.Where("id = ?", md.Query("id"))
|
|
|
|
}
|
|
|
|
rows, err := md.DB.Query("SELECT id, privileges, description, last_updated FROM tokens "+
|
|
|
|
wc.Clause+common.Paginate(md.Query("p"), md.Query("l"), 50), wc.Params...)
|
2017-07-25 08:41:09 +00:00
|
|
|
|
2017-07-26 17:17:25 +00:00
|
|
|
if err != nil {
|
2016-06-14 09:32:11 +00:00
|
|
|
return Err500
|
|
|
|
}
|
|
|
|
var r tokenResponse
|
|
|
|
for rows.Next() {
|
|
|
|
var t token
|
2016-12-01 22:16:36 +00:00
|
|
|
err = rows.Scan(&t.ID, &t.Privileges, &t.Description, &t.LastUpdated)
|
2016-06-14 09:32:11 +00:00
|
|
|
if err != nil {
|
|
|
|
md.Err(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.Tokens = append(r.Tokens, t)
|
|
|
|
}
|
|
|
|
r.Code = 200
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-06-17 16:11:10 +00:00
|
|
|
type oauthClient struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
OwnerID int `json:"owner_id"`
|
|
|
|
Avatar string `json:"avatar"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan scans the extra in the mysql table into Name, OwnerID and Avatar.
|
|
|
|
func (o *oauthClient) Scan(src interface{}) error {
|
|
|
|
var s []byte
|
|
|
|
switch x := src.(type) {
|
|
|
|
case string:
|
|
|
|
s = []byte(x)
|
|
|
|
case []byte:
|
|
|
|
s = x
|
|
|
|
default:
|
|
|
|
return errors.New("Can't scan non-string")
|
|
|
|
}
|
|
|
|
|
|
|
|
var vals [3]string
|
|
|
|
err := json.Unmarshal(s, &vals)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Name = vals[0]
|
|
|
|
o.OwnerID, _ = strconv.Atoi(vals[1])
|
|
|
|
o.Avatar = vals[2]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type bearerToken struct {
|
|
|
|
Client oauthClient `json:"client"`
|
|
|
|
Scope string `json:"scope"`
|
|
|
|
Privileges common.Privileges `json:"privileges"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:01:30 +00:00
|
|
|
type tokenSingleResponse struct {
|
|
|
|
common.ResponseBase
|
|
|
|
token
|
|
|
|
}
|
|
|
|
|
2017-06-17 16:11:10 +00:00
|
|
|
type bearerTokenSingleResponse struct {
|
|
|
|
common.ResponseBase
|
|
|
|
bearerToken
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:01:30 +00:00
|
|
|
// TokenSelfGET retrieves information about the token the user is connecting with.
|
|
|
|
func TokenSelfGET(md common.MethodData) common.CodeMessager {
|
2016-08-09 17:22:41 +00:00
|
|
|
if md.ID() == 0 {
|
|
|
|
return common.SimpleResponse(404, "How are we supposed to find the token you're using if you ain't even using one?!")
|
|
|
|
}
|
2017-06-17 16:11:10 +00:00
|
|
|
if md.IsBearer() {
|
|
|
|
return getBearerToken(md)
|
|
|
|
}
|
2016-06-14 10:01:30 +00:00
|
|
|
var r tokenSingleResponse
|
|
|
|
// md.User.ID = token id, userid would have been md.User.UserID. what a clusterfuck
|
2017-06-17 16:11:10 +00:00
|
|
|
err := md.DB.QueryRow("SELECT id, privileges, description, last_updated FROM tokens WHERE id = ?", md.User.ID).Scan(
|
2016-12-01 22:16:36 +00:00
|
|
|
&r.ID, &r.Privileges, &r.Description, &r.LastUpdated,
|
2016-06-14 10:01:30 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
md.Err(err)
|
|
|
|
return Err500
|
|
|
|
}
|
|
|
|
r.Code = 200
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-06-17 16:11:10 +00:00
|
|
|
func getBearerToken(md common.MethodData) common.CodeMessager {
|
|
|
|
var b bearerTokenSingleResponse
|
|
|
|
err := md.DB.
|
|
|
|
QueryRow(`
|
|
|
|
SELECT t.scope, t.created_at, c.id, c.extra
|
|
|
|
FROM osin_access t INNER JOIN osin_client c
|
|
|
|
WHERE t.access_token = ?
|
|
|
|
`, fmt.Sprintf("%x", sha256.Sum256([]byte(md.User.Value)))).Scan(
|
|
|
|
&b.Scope, &b.Created, &b.Client.ID, &b.Client,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
md.Err(err)
|
|
|
|
return Err500
|
|
|
|
}
|
|
|
|
b.Code = 200
|
|
|
|
b.Privileges = md.User.TokenPrivileges
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-11-21 16:04:27 +00:00
|
|
|
// TokenFixPrivilegesPOST fixes the privileges on the token of the given user,
|
2016-06-14 09:32:11 +00:00
|
|
|
// or of all the users if no user is given.
|
2016-11-21 16:04:27 +00:00
|
|
|
func TokenFixPrivilegesPOST(md common.MethodData) common.CodeMessager {
|
2016-08-15 17:59:46 +00:00
|
|
|
id := common.Int(md.Query("id"))
|
|
|
|
if md.Query("id") == "self" {
|
2016-06-14 09:32:11 +00:00
|
|
|
id = md.ID()
|
|
|
|
}
|
|
|
|
go fixPrivileges(id, md.DB)
|
|
|
|
return common.SimpleResponse(200, "Privilege fixing started!")
|
|
|
|
}
|
|
|
|
|
2016-08-15 11:37:03 +00:00
|
|
|
func fixPrivileges(user int, db *sqlx.DB) {
|
2016-06-14 09:32:11 +00:00
|
|
|
var wc string
|
|
|
|
var params = make([]interface{}, 0, 1)
|
|
|
|
if user != 0 {
|
|
|
|
// dirty, but who gives a shit
|
|
|
|
wc = "WHERE user = ?"
|
|
|
|
params = append(params, user)
|
|
|
|
}
|
|
|
|
rows, err := db.Query(`
|
|
|
|
SELECT
|
2017-07-25 08:39:37 +00:00
|
|
|
tokens.id, tokens.privileges, users.privileges
|
2016-06-14 09:32:11 +00:00
|
|
|
FROM tokens
|
|
|
|
LEFT JOIN users ON users.id = tokens.user
|
|
|
|
`+wc, params...)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
schiavo.Bunker.Send(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
2016-08-27 10:04:12 +00:00
|
|
|
id int
|
|
|
|
privsRaw uint64
|
|
|
|
privs common.Privileges
|
|
|
|
newPrivs common.Privileges
|
|
|
|
privilegesRaw uint64
|
2016-06-14 09:32:11 +00:00
|
|
|
)
|
2016-08-27 10:04:12 +00:00
|
|
|
err := rows.Scan(&id, &privsRaw, &privilegesRaw)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
privileges := common.UserPrivileges(privilegesRaw)
|
2016-06-14 09:32:11 +00:00
|
|
|
privs = common.Privileges(privsRaw)
|
2016-07-03 22:06:23 +00:00
|
|
|
newPrivs = privs.CanOnly(privileges)
|
2016-06-14 09:32:11 +00:00
|
|
|
if newPrivs != privs {
|
|
|
|
_, err := db.Exec("UPDATE tokens SET privileges = ? WHERE id = ? LIMIT 1", uint64(newPrivs), id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
schiavo.Bunker.Send(err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|