2021-04-02 09:24:00 +00:00
|
|
|
package goscrobble
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OauthToken struct {
|
|
|
|
UserUUID string `json:"user"`
|
|
|
|
Service string `json:"service"`
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
Expiry time.Time `json:"expiry"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
LastSynced time.Time `json:"last_synced"`
|
2021-04-09 21:49:32 +00:00
|
|
|
URL string `json:"url"`
|
2021-04-02 09:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getOauthToken(userUuid string, service string) (OauthToken, error) {
|
|
|
|
var oauth OauthToken
|
|
|
|
|
2021-12-25 09:24:47 +00:00
|
|
|
err := db.QueryRow("SELECT user, service, access_token, refresh_token, expiry, username, last_synced, url FROM oauth_tokens "+
|
|
|
|
"WHERE user = $1 AND service = $2",
|
2021-04-09 21:49:32 +00:00
|
|
|
userUuid, service).Scan(&oauth.UserUUID, &oauth.Service, &oauth.AccessToken, &oauth.RefreshToken, &oauth.Expiry, &oauth.Username, &oauth.LastSynced, &oauth.URL)
|
2021-04-02 09:24:00 +00:00
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return oauth, errors.New("No token for user")
|
|
|
|
}
|
|
|
|
|
|
|
|
return oauth, nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 21:49:32 +00:00
|
|
|
func insertOauthToken(userUuid string, service string, token string, refresh string, expiry time.Time, username string, lastSynced time.Time, url string) error {
|
2021-12-25 09:24:47 +00:00
|
|
|
_, err := db.Exec("REPLACE INTO oauth_tokens (user, service, access_token, refresh_token, expiry, username, last_synced, url) "+
|
|
|
|
"VALUES ($1,$2,$3,$4,$5,$6,$7,$8)", userUuid, service, token, refresh, expiry, username, lastSynced, url)
|
2021-04-02 09:24:00 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeOauthToken(userUuid string, service string) error {
|
2021-12-25 09:24:47 +00:00
|
|
|
_, err := db.Exec("DELETE FROM oauth_tokens WHERE user = $1 AND service = $2", userUuid, service)
|
2021-04-02 09:24:00 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|