fix some api key stuff

This commit is contained in:
Wim Brand
2020-02-21 14:09:13 +01:00
parent 7a67d8c33f
commit 4de17609bb
7 changed files with 21 additions and 14 deletions

View File

@@ -22,8 +22,8 @@ type PixelAPI struct {
} }
// New creates a new Pixeldrain API client to query the Pixeldrain API with // New creates a new Pixeldrain API client to query the Pixeldrain API with
func New(apiEndpoint, apiKey string) *PixelAPI { func New(apiEndpoint string) *PixelAPI {
return &PixelAPI{apiEndpoint, apiKey, ""} return &PixelAPI{apiEndpoint: apiEndpoint}
} }
// Error is either an error that occurred during the API request // Error is either an error that occurred during the API request

View File

@@ -3,7 +3,6 @@ package webcontroller
import ( import (
"net/http" "net/http"
"fornaxian.com/pixeldrain-api/util"
"github.com/Fornaxian/log" "github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
@@ -14,7 +13,6 @@ func (wc *WebController) serveAdClick(w http.ResponseWriter, r *http.Request, p
// Get a view token // Get a view token
td := wc.newTemplateData(w, r) td := wc.newTemplateData(w, r)
td.PixelAPI.RealIP = util.RemoteAddress(r)
vt := viewTokenOrBust(td.PixelAPI) vt := viewTokenOrBust(td.PixelAPI)
// Log a view on the file // Log a view on the file

View File

@@ -23,9 +23,10 @@ func (wc *WebController) serveFilePreview(w http.ResponseWriter, r *http.Request
return return
} }
apikey, _ := wc.getAPIKey(r) api := pixelapi.New(wc.apiURLInternal)
api := pixelapi.New(wc.apiURLInternal, apikey) api.APIKey, _ = wc.getAPIKey(r)
api.RealIP = util.RemoteAddress(r) api.RealIP = util.RemoteAddress(r)
file, err := api.GetFileInfo(p.ByName("id")) // TODO: Error handling file, err := api.GetFileInfo(p.ByName("id")) // TODO: Error handling
if err != nil { if err != nil {
wc.serveNotFound(w, r) wc.serveNotFound(w, r)

View File

@@ -43,20 +43,27 @@ type TemplateData struct {
Form Form Form Form
} }
func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) *TemplateData { func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) (t *TemplateData) {
var t = &TemplateData{ t = &TemplateData{
Authenticated: false, Authenticated: false,
Username: "", Username: "",
UserAgent: r.UserAgent(), UserAgent: r.UserAgent(),
Style: userStyle(r), Style: userStyle(r),
UserStyle: template.CSS(userStyle(r).String()), UserStyle: template.CSS(userStyle(r).String()),
APIEndpoint: template.URL(wc.apiURLExternal), APIEndpoint: template.URL(wc.apiURLExternal),
PixelAPI: pixelapi.New(wc.apiURLInternal),
Hostname: template.HTML(wc.hostname), Hostname: template.HTML(wc.hostname),
URLQuery: r.URL.Query(), URLQuery: r.URL.Query(),
} }
// Use the user's IP address for making requests
t.PixelAPI.RealIP = util.RemoteAddress(r)
// If the user is authenticated we'll indentify him and put the user info
// into the templatedata. This is used for putting the username in the menu
// and stuff like that
if key, err := wc.getAPIKey(r); err == nil { if key, err := wc.getAPIKey(r); err == nil {
t.PixelAPI = pixelapi.New(wc.apiURLInternal, key) t.PixelAPI.APIKey = key // Use the user's API key for all requests
uinf, err := t.PixelAPI.UserInfo() uinf, err := t.PixelAPI.UserInfo()
if err != nil { if err != nil {
// This session key doesn't work, or the backend is down, user // This session key doesn't work, or the backend is down, user
@@ -91,8 +98,6 @@ func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request)
t.Authenticated = true t.Authenticated = true
t.Username = uinf.Username t.Username = uinf.Username
t.Email = uinf.Email t.Email = uinf.Email
} else {
t.PixelAPI = pixelapi.New(wc.apiURLInternal, "")
} }
return t return t

View File

@@ -16,7 +16,8 @@ func (wc *WebController) serveLogout(
p httprouter.Params, p httprouter.Params,
) { ) {
if key, err := wc.getAPIKey(r); err == nil { if key, err := wc.getAPIKey(r); err == nil {
var api = pixelapi.New(wc.apiURLInternal, key) var api = pixelapi.New(wc.apiURLInternal)
api.APIKey = key
if err = api.UserSessionDestroy(key); err != nil { if err = api.UserSessionDestroy(key); err != nil {
log.Warn("logout failed for session '%s': %s", key, err) log.Warn("logout failed for session '%s': %s", key, err)
} }

View File

@@ -133,7 +133,9 @@ func (wc *WebController) serveEmailConfirm(
) { ) {
var status string var status string
if key, err := wc.getAPIKey(r); err == nil { if key, err := wc.getAPIKey(r); err == nil {
err = pixelapi.New(wc.apiURLInternal, key).UserEmailResetConfirm(r.FormValue("key")) api := pixelapi.New(wc.apiURLInternal)
api.APIKey = key
err = api.UserEmailResetConfirm(r.FormValue("key"))
if err != nil && err.Error() == "not_found" { if err != nil && err.Error() == "not_found" {
status = "not_found" status = "not_found"
} else if err != nil { } else if err != nil {

View File

@@ -241,7 +241,7 @@ func (wc *WebController) getAPIKey(r *http.Request) (key string, err error) {
func (wc *WebController) captchaKey() string { func (wc *WebController) captchaKey() string {
// This only runs on the first request // This only runs on the first request
if wc.captchaSiteKey == "" { if wc.captchaSiteKey == "" {
var api = pixelapi.New(wc.apiURLInternal, "") var api = pixelapi.New(wc.apiURLInternal)
capt, err := api.GetRecaptcha() capt, err := api.GetRecaptcha()
if err != nil { if err != nil {
log.Error("Error getting recaptcha key: %s", err) log.Error("Error getting recaptcha key: %s", err)