-
Notifications
You must be signed in to change notification settings - Fork 11
/
tokens.go
120 lines (112 loc) · 3.82 KB
/
tokens.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
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
package api
import (
"context"
"encoding/json"
"errors"
"net/http"
"github.com/micromdm/nanodep/client"
"github.com/micromdm/nanodep/log"
"github.com/micromdm/nanodep/log/ctxlog"
)
var CKMismatch = errors.New("mismatched consumer key")
type AuthTokensStore interface {
client.AuthTokensRetriever
AuthTokensStorer
}
type AuthTokensStorer interface {
StoreAuthTokens(ctx context.Context, name string, tokens *client.OAuth1Tokens) error
}
// RetrieveAuthTokensHandler returns the DEP server OAuth1 tokens for the DEP
// name in the path.
//
// Note the whole URL path is used as the DEP name. This necessitates
// stripping the URL prefix before using this handler. Also note we expose Go
// errors to the output as this is meant for "API" users.
func RetrieveAuthTokensHandler(store client.AuthTokensRetriever, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
if r.URL.Path == "" {
logger.Info("msg", "DEP name check", "err", "missing DEP name")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
logger = logger.With("name", r.URL.Path)
tokens, err := store.RetrieveAuthTokens(r.Context(), r.URL.Path)
if err != nil {
logger.Info("msg", "retrieving auth tokens", "err", err)
jsonError(w, err)
return
}
w.Header().Set("Content-type", "application/json")
err = json.NewEncoder(w).Encode(tokens)
if err != nil {
logger.Info("msg", "encoding response body", "err", err)
return
}
}
}
// StoreAuthTokensHandler reads DEP server OAuth1 tokens as a JSON body and
// saves them using store.
//
// Note the whole URL path is used as the DEP name. This necessitates
// stripping the URL prefix before using this handler. Also note we expose Go
// errors to the output as this is meant for "API" users.
func StoreAuthTokensHandler(store AuthTokensStore, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
if r.URL.Path == "" {
logger.Info("msg", "DEP name check", "err", "missing DEP name")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
force := r.URL.Query().Get("force") == "1"
logger = logger.With("name", r.URL.Path, "force", force)
tokens := new(client.OAuth1Tokens)
err := json.NewDecoder(r.Body).Decode(tokens)
if err != nil {
logger.Info("msg", "decoding request body", "err", err)
jsonError(w, err)
return
}
defer r.Body.Close()
storeTokens(r.Context(), logger, r.URL.Path, tokens, store, w, force)
}
}
func storeTokens(ctx context.Context, logger log.Logger, name string, tokens *client.OAuth1Tokens, store AuthTokensStore, w http.ResponseWriter, force bool) {
if !tokens.Valid() {
logger.Info("msg", "checking auth token validity", "err", "invalid tokens")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
logger = logger.With("consumer_key", tokens.ConsumerKey)
if !force {
prevTokens, err := store.RetrieveAuthTokens(ctx, name)
if err != nil {
logger.Debug(
"msg", "error retrieving auth tokens; proceeding to store",
"err", err,
)
} else if prevTokens != nil && prevTokens.ConsumerKey != tokens.ConsumerKey {
logger.Info(
"msg", "checking consumer key (use force to bypass)",
"err", CKMismatch,
"prev_consumer_key", prevTokens.ConsumerKey,
)
jsonError(w, CKMismatch)
return
}
}
err := store.StoreAuthTokens(ctx, name, tokens)
if err != nil {
logger.Info("msg", "storing auth tokens", "err", err)
jsonError(w, err)
return
}
logger.Debug("msg", "stored auth tokens")
w.Header().Set("Content-type", "application/json")
err = json.NewEncoder(w).Encode(tokens)
if err != nil {
logger.Info("msg", "encoding response body", "err", err)
return
}
}