File size: 2,614 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package mockslack

import (
	"context"
	"net/http"
	"strings"
)

type contextKey int

const (
	contextKeyToken contextKey = iota
)

// AuthToken represents a state of authorization with the Slack server.
type AuthToken struct {
	ID     string
	Scopes []string
	User   string
	IsBot  bool
}

// WithToken will return a new context authorized for API calls with the given AuthToken.
func WithToken(ctx context.Context, tok *AuthToken) context.Context {
	if tok == nil {
		return ctx
	}
	cpy := *tok
	cpy.Scopes = make([]string, len(tok.Scopes))
	copy(cpy.Scopes, tok.Scopes)
	return context.WithValue(ctx, contextKeyToken, cpy)
}

// ContextToken will return a copy of the AuthToken from the given context.
func ContextToken(ctx context.Context) *AuthToken {
	tok, ok := ctx.Value(contextKeyToken).(AuthToken)
	if !ok {
		return nil
	}
	return &tok
}

func tokenID(ctx context.Context) string {
	tok := ContextToken(ctx)
	if tok == nil {
		return ""
	}
	return tok.ID
}
func userID(ctx context.Context) string {
	tok := ContextToken(ctx)
	if tok == nil || tok.IsBot {
		return ""
	}

	return tok.User
}
func botID(ctx context.Context) string {
	tok := ContextToken(ctx)
	if tok == nil || !tok.IsBot {
		return ""
	}

	return tok.User
}

type scopeError struct {
	response
	Needed   string `json:"needed"`
	Provided string `json:"provided"`
}

func (r response) Error() string { return r.Err }

func hasScope(ctx context.Context, scopes ...string) bool {
	return ContextToken(ctx).hasScope(scopes...)
}

func (tok *AuthToken) hasScope(scopes ...string) bool {
	if tok == nil {
		return false
	}
	for _, scope := range scopes {
		for _, tokenScope := range tok.Scopes {
			if scope == tokenScope {
				return true
			}
		}
	}

	return false
}

func checkPermission(ctx context.Context, scopes ...string) error {
	tok := ContextToken(ctx)
	if tok == nil {
		return response{Err: "not_authed"}
	}

	if len(scopes) == 0 || tok.hasScope(scopes...) {
		return nil
	}

	return &scopeError{
		Needed:   strings.Join(scopes, ","),
		Provided: strings.Join(tok.Scopes, ","),
		response: response{Err: "missing_scope"},
	}
}

func (s *Server) tokenMiddleware(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		tok := s.token(req.FormValue("token"))
		if tok == nil && strings.HasPrefix(req.Header.Get("Authorization"), "Bearer ") {
			tok = s.token(strings.TrimPrefix(req.Header.Get("Authorization"), "Bearer "))
		}
		if c, _ := req.Cookie(TokenCookieName); tok == nil && c != nil {
			tok = s.token(c.Value)
		}

		next(w, req.WithContext(WithToken(req.Context(), tok)))
	}
}