Repository navigation
Expand file tree
/
Copy pathauth.go
More file actions
209 lines (192 loc) · 6.17 KB
/
Copy pathauth.go
File metadata and controls
209 lines (192 loc) · 6.17 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"strings"
"time"
"github.com/datahearth/streamline/extensions/sdk"
"github.com/streamline-hq/streamline-subsonic-ext/creds"
"github.com/streamline-hq/streamline-subsonic-ext/envelope"
)
// permission is a Subsonic role an admin switches per account. Each gates
// Subsonic calls only, never anything in Streamline's web UI.
type permission string
const (
permStream permission = "streamRole"
permDownload permission = "downloadRole"
permPlaylists permission = "playlistRole"
permShare permission = "shareRole"
permRate permission = "commentRole"
permChangePassword permission = "settingsRole"
)
// account is the signed-in Subsonic user: who Streamline says they are and
// what this extension lets them do.
type account struct {
ID uint32
Email string
Role string
DisplayName string
CanStream bool
CanDownload bool
CanManagePlaylists bool
CanShare bool
CanRate bool
CanChangePassword bool
// MaxBitRate is the kbps ceiling on every stream; 0 is none.
MaxBitRate uint16
// Locked is Streamline's lockout after failed sign-ins.
Locked bool
}
func (a *account) allowed(p permission) bool {
switch p {
case permStream:
return a.CanStream
case permDownload:
return a.CanDownload
case permPlaylists:
return a.CanManagePlaylists
case permShare:
return a.CanShare
case permRate:
return a.CanRate
case permChangePassword:
return a.CanChangePassword
}
return false
}
func (a *account) isAdmin() bool { return a.Role == "admin" }
// touchInterval is how often a successful sign-in is recorded, so a stream of
// requests costs one write in five minutes.
const touchInterval = 5 * time.Minute
const accountColumns = `COALESCE(a.can_stream, 1), COALESCE(a.can_download, 1),
COALESCE(a.can_manage_playlists, 1), COALESCE(a.can_share, 1),
COALESCE(a.can_rate, 1), COALESCE(a.can_change_password, 1),
COALESCE(a.max_bit_rate, 0)`
// scanAccount reads l.user_id, l.email, u.role, u.display_name, then
// accountColumns, then u.locked.
func scanAccount(r row) *account {
return &account{
ID: r.uint(0),
Email: r.str(1),
Role: r.str(2),
DisplayName: r.str(3),
CanStream: r.bool(4),
CanDownload: r.bool(5),
CanManagePlaylists: r.bool(6),
CanShare: r.bool(7),
CanRate: r.bool(8),
CanChangePassword: r.bool(9),
MaxBitRate: uint16(min(max(r.int(10), 0), maxBitRateCeiling)),
Locked: r.bool(11),
}
}
// loadAccount reads an account by user id, whether or not it has a password.
// found is false for an id Streamline does not have.
func loadAccount(userID uint32) (acc *account, found bool, err error) {
r, found, err := queryRow(
`SELECT l.user_id, l.email, u.role, u.display_name, `+accountColumns+`, u.locked
FROM streamline_user_logins l
JOIN streamline_users u ON u.id = l.user_id
LEFT JOIN accounts a ON a.user_id = l.user_id
WHERE l.user_id = ?`,
userID,
)
if err != nil || !found {
return nil, false, err
}
return scanAccount(r), true, nil
}
// loadAccountByEmail reads an account by the login the Subsonic u parameter
// carries; the user name is the email, matched without regard to case.
func loadAccountByEmail(email string) (acc *account, found bool, err error) {
r, found, err := queryRow(
`SELECT l.user_id, l.email, u.role, u.display_name, `+accountColumns+`, u.locked
FROM streamline_user_logins l
JOIN streamline_users u ON u.id = l.user_id
LEFT JOIN accounts a ON a.user_id = l.user_id
WHERE l.email = ?`,
strings.ToLower(strings.TrimSpace(email)),
)
if err != nil || !found {
return nil, false, err
}
return scanAccount(r), true, nil
}
// authenticate resolves the caller from the u parameter and checks the
// credentials. An unknown user, no Subsonic password and a wrong password all
// answer error 40 with one message, so the response does not reveal which
// logins exist. An apiKey answers error 42, as this server offers none, two
// mechanisms at once error 43, and a missing p, t or s error 10, all before
// any login is looked up.
func (c *reqCtx) authenticate() error {
switch creds.Detect(c.params) {
case creds.SchemeConflict:
return envelope.ConflictingAuth()
case creds.SchemeAPIKey:
return envelope.AuthNotSupported()
case creds.SchemeUser:
}
login := c.param("u")
if login == "" {
return envelope.MissingParam("u")
}
if name := creds.MissingCredential(c.params); name != "" {
return envelope.MissingParam(name)
}
acc, found, err := loadAccountByEmail(login)
if err != nil {
return err
}
if !found || acc.Locked {
return envelope.WrongCredentials()
}
stored, err := storedPassword(acc.ID)
if err != nil {
return err
}
if !creds.Check(stored, c.params) {
return envelope.WrongCredentials()
}
c.user = acc
touchAccount(acc.ID, c.param("c"))
return nil
}
func storedPassword(userID uint32) (string, error) {
r, found, err := queryRow(
"SELECT password FROM accounts WHERE user_id = ?", userID,
)
if err != nil || !found {
return "", err
}
return r.str(0), nil
}
// touchAccount records a successful sign-in and the client that made it. It
// is bookkeeping: a failure is logged and never fails the request.
func touchAccount(userID uint32, client string) {
now := time.Now().UTC()
_, err := exec(
`UPDATE accounts SET last_used_at = ?, last_client = ?
WHERE user_id = ? AND (last_used_at IS NULL OR last_used_at < ?)`,
formatStored(now),
creds.SanitizeClient(client),
userID,
formatStored(now.Add(-touchInterval)),
)
if err != nil {
sdk.Log("warn", "recording a sign-in failed", "error", err.Error())
}
}
// storePassword makes password the account's Subsonic password and restarts
// its "last used" record. It is the one writer of the column, for the
// credential card and for changePassword alike.
func storePassword(userID uint32, password string) error {
_, err := exec(
`INSERT INTO accounts(user_id, password, password_created_at)
VALUES(?, ?, ?)
ON CONFLICT(user_id) DO UPDATE SET
password = excluded.password,
password_created_at = excluded.password_created_at,
last_used_at = NULL,
last_client = ''`,
userID, password, nowStored(),
)
return err
}