-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_users.go
77 lines (71 loc) · 2.18 KB
/
spotify_users.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package data_store
import (
"fmt"
"github.com/cheshire137/scrobble-saver/pkg/util"
)
type SpotifyUser struct {
Id string
AccessToken string
RefreshToken string
Scopes string
ExpiresIn int
}
func (ds *DataStore) LoadSpotifyUser(id string) (*SpotifyUser, error) {
query := `SELECT access_token, refresh_token, scopes, expires_in FROM spotify_users WHERE id = ?`
stmt, err := ds.db.Prepare(query)
if err != nil {
return nil, err
}
var encryptedAccessToken, encryptedRefreshToken, scopes string
var expiresIn int
err = stmt.QueryRow(id).Scan(&encryptedAccessToken, &encryptedRefreshToken, &scopes, &expiresIn)
if err != nil {
return nil, err
}
accessToken, err := util.Decrypt(encryptedAccessToken, ds.secret)
if err != nil {
return nil, err
}
refreshToken, err := util.Decrypt(encryptedRefreshToken, ds.secret)
if err != nil {
return nil, err
}
spotifyUser := &SpotifyUser{Id: id, AccessToken: accessToken, RefreshToken: refreshToken, Scopes: scopes,
ExpiresIn: expiresIn}
return spotifyUser, nil
}
func (ds *DataStore) UpsertSpotifyUser(spotifyUser *SpotifyUser) error {
query := `INSERT INTO spotify_users (id, access_token, refresh_token, scopes, expires_in)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT (id) DO UPDATE SET access_token = excluded.access_token, refresh_token = excluded.refresh_token,
scopes = excluded.scopes, expires_in = excluded.expires_in`
stmt, err := ds.db.Prepare(query)
if err != nil {
return err
}
encryptedAccessToken, err := util.Encrypt(spotifyUser.AccessToken, ds.secret)
if err != nil {
return err
}
encryptedRefreshToken, err := util.Encrypt(spotifyUser.RefreshToken, ds.secret)
if err != nil {
return err
}
fmt.Println("encrypted refresh token:" + encryptedRefreshToken)
_, err = stmt.Exec(spotifyUser.Id, encryptedAccessToken, encryptedRefreshToken, spotifyUser.Scopes,
spotifyUser.ExpiresIn)
if err != nil {
return err
}
return nil
}
func (ds *DataStore) createSpotifyUsersTable() error {
query := `CREATE TABLE IF NOT EXISTS spotify_users (
id TEXT PRIMARY KEY,
access_token TEXT NOT NULL,
refresh_token TEXT NOT NULL,
scopes TEXT NOT NULL,
expires_in INTEGER NOT NULL
)`
return ds.createTable(query)
}