diff --git a/http/api/tokenpki.go b/http/api/tokenpki.go index 4da37d5..37b689b 100644 --- a/http/api/tokenpki.go +++ b/http/api/tokenpki.go @@ -142,18 +142,6 @@ func DecryptTokenPKIHandler(store TokenPKIRetriever, tokenStore AuthTokensStorer http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return } - err = tokenStore.StoreAuthTokens(r.Context(), r.URL.Path, 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 - } + storeTokens(r.Context(), logger, r.URL.Path, tokens, tokenStore, w) } } diff --git a/http/api/tokens.go b/http/api/tokens.go index 7e597a5..c7068bb 100644 --- a/http/api/tokens.go +++ b/http/api/tokens.go @@ -72,18 +72,23 @@ func StoreAuthTokensHandler(store AuthTokensStorer, logger log.Logger) http.Hand http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return } - err = store.StoreAuthTokens(r.Context(), r.URL.Path, 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 - } + storeTokens(r.Context(), logger, r.URL.Path, tokens, store, w) + } +} + +func storeTokens(ctx context.Context, logger log.Logger, name string, tokens *client.OAuth1Tokens, store AuthTokensStorer, w http.ResponseWriter) { + logger = logger.With("consumer_key", tokens.ConsumerKey) + 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 } }