-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
auth.go
75 lines (64 loc) · 1.88 KB
/
auth.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
package resolver
import (
"context"
"fmt"
"github.com/smartcontractkit/chainlink/v2/core/sessions"
"github.com/smartcontractkit/chainlink/v2/core/web/auth"
)
// Authenticates the user from the session cookie, presence of user inherently provides 'view' access.
func authenticateUser(ctx context.Context) error {
if _, ok := auth.GetGQLAuthenticatedSession(ctx); !ok {
return unauthorizedError{}
}
return nil
}
// Authenticates the user from the session cookie and asserts at least 'run' role.
func authenticateUserCanRun(ctx context.Context) error {
session, ok := auth.GetGQLAuthenticatedSession(ctx)
if !ok {
return unauthorizedError{}
}
if session.User.Role == sessions.UserRoleView {
return RoleNotPermittedErr{session.User.Role}
}
return nil
}
// Authenticates the user from the session cookie and asserts at least 'edit' role.
func authenticateUserCanEdit(ctx context.Context) error {
session, ok := auth.GetGQLAuthenticatedSession(ctx)
if !ok {
return unauthorizedError{}
}
switch session.User.Role {
case sessions.UserRoleView, sessions.UserRoleRun:
return RoleNotPermittedErr{session.User.Role}
default:
}
return nil
}
// Authenticates the user from the session cookie and asserts has 'admin' role
func authenticateUserIsAdmin(ctx context.Context) error {
session, ok := auth.GetGQLAuthenticatedSession(ctx)
if !ok {
return unauthorizedError{}
}
if session.User.Role != sessions.UserRoleAdmin {
return RoleNotPermittedErr{session.User.Role}
}
return nil
}
type unauthorizedError struct{}
func (e unauthorizedError) Error() string {
return "Unauthorized"
}
func (e unauthorizedError) Extensions() map[string]interface{} {
return map[string]interface{}{
"code": "UNAUTHORIZED",
}
}
type RoleNotPermittedErr struct {
Role sessions.UserRole
}
func (e RoleNotPermittedErr) Error() string {
return fmt.Sprintf("Not permitted with current role: %s", e.Role)
}