get recaptcha site key from api

This commit is contained in:
2018-09-19 22:26:52 +02:00
parent 616fe14ba3
commit 5207abd15f
9 changed files with 84 additions and 42 deletions

View File

@@ -1,13 +1,16 @@
package pixelapi package pixelapi
// Recaptcha stores the reCaptcha site key
type Recaptcha struct { type Recaptcha struct {
SiteKey string `json:"site_key"` SiteKey string `json:"site_key"`
} }
func (p *PixelAPI) GetRecaptcha() (resp *Recaptcha, err error) { // GetRecaptcha gets the reCaptcha site key from the pixelapi server. If
err = p.jsonRequest("GET", p.apiEndpoint+"/misc/recpatcha", resp) // reCaptcha is disabled the key will be empty
func (p *PixelAPI) GetRecaptcha() (resp Recaptcha, err error) {
err = p.jsonRequest("GET", p.apiEndpoint+"/misc/recaptcha", &resp)
if err != nil { if err != nil {
return nil, err return resp, err
} }
return resp, nil return resp, nil
} }

View File

@@ -11,6 +11,8 @@ import (
"github.com/Fornaxian/log" "github.com/Fornaxian/log"
) )
var client = &http.Client{}
// PixelAPI is the Pixeldrain API client // PixelAPI is the Pixeldrain API client
type PixelAPI struct { type PixelAPI struct {
apiEndpoint string apiEndpoint string
@@ -59,7 +61,6 @@ func (p *PixelAPI) jsonRequest(method, url string, target interface{}) error {
req.SetBasicAuth("", p.apiKey) req.SetBasicAuth("", p.apiKey)
} }
client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return Error{ return Error{
@@ -106,8 +107,6 @@ func (p *PixelAPI) getRaw(url string) (io.ReadCloser, error) {
req.SetBasicAuth("", p.apiKey) req.SetBasicAuth("", p.apiKey)
} }
client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -130,7 +129,6 @@ func (p *PixelAPI) postForm(url string, vals url.Values, target interface{}) err
req.SetBasicAuth("", p.apiKey) req.SetBasicAuth("", p.apiKey)
} }
client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return &Error{ return &Error{

View File

@@ -204,6 +204,12 @@ var UploadWorker = /** @class */ (function () {
}); });
}; };
UploadWorker.prototype.setHistoryCookie = function (id) { UploadWorker.prototype.setHistoryCookie = function (id) {
// Make sure the user is not logged in, for privacy. This keeps the
// files uploaded while logged in and anonymously uploaded files
// separated
if (Cookie.read("pd_auth_key") !== null) {
return;
}
var uc = Cookie.read("pduploads"); var uc = Cookie.read("pduploads");
// First upload in this browser // First upload in this browser
if (uc === null) { if (uc === null) {

View File

@@ -117,6 +117,13 @@ class UploadWorker {
} }
private setHistoryCookie(id: string){ private setHistoryCookie(id: string){
// Make sure the user is not logged in, for privacy. This keeps the
// files uploaded while logged in and anonymously uploaded files
// separated
if (Cookie.read("pd_auth_key") !== null) {
return;
}
var uc = Cookie.read("pduploads") var uc = Cookie.read("pduploads")
// First upload in this browser // First upload in this browser

View File

@@ -43,13 +43,13 @@
<hr/> <hr/>
</td> </td>
</tr> </tr>
<tr class="form"> {{if ne .Other "none"}}<tr class="form">
<td> <td>
Turing test<br/> Turing test<br/>
(Click the white box) (Click the white box)
</td> </td>
<td style="text-align: center;"> <td style="text-align: center;">
<div class="g-recaptcha" data-theme="dark" data-sitekey="6Lfbzz4UAAAAAAaBgox1R7jU0axiGneLDkOA-PKf"></div> <div class="g-recaptcha" data-theme="dark" data-sitekey="{{.Other}}"></div>
</td> </td>
</tr> </tr>
<tr class="form"> <tr class="form">
@@ -58,7 +58,7 @@
an evil robot that is trying to flood the website an evil robot that is trying to flood the website
with fake accounts<br/><hr/> with fake accounts<br/><hr/>
</td> </td>
</tr> </tr>{{end}}
<tr class="form"> <tr class="form">
<td colspan="2" style="text-align: right;"> <td colspan="2" style="text-align: right;">
<input type="submit" value="Register" class="button_highlight"/> <input type="submit" value="Register" class="button_highlight"/>

View File

@@ -1,9 +1,10 @@
package templates package webcontroller
import ( import (
"html/template" "html/template"
"os" "os"
"path/filepath" "path/filepath"
"time"
"github.com/Fornaxian/log" "github.com/Fornaxian/log"
) )
@@ -17,7 +18,7 @@ type TemplateManager struct {
debugModeEnabled bool debugModeEnabled bool
} }
func New(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager { func NewTemplateManager(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager {
return &TemplateManager{ return &TemplateManager{
templateDir: templateDir, templateDir: templateDir,
externalAPIEndpoint: externalAPIEndpoint, externalAPIEndpoint: externalAPIEndpoint,
@@ -65,3 +66,23 @@ func (tm *TemplateManager) Get() *template.Template {
} }
return tm.templates return tm.templates
} }
func (tm *TemplateManager) funcMap() template.FuncMap {
return template.FuncMap{
"bgPatternCount": tm.bgPatternCount,
"debugMode": tm.debugMode,
"apiUrl": tm.apiURL,
}
}
func (tm *TemplateManager) bgPatternCount() uint8 {
return uint8(time.Now().UnixNano() % 17)
}
func (tm *TemplateManager) debugMode() bool {
return tm.debugModeEnabled
}
func (tm *TemplateManager) apiURL() string {
return tm.externalAPIEndpoint
}

View File

@@ -1,26 +0,0 @@
package templates
import (
"html/template"
"time"
)
func (tm *TemplateManager) funcMap() template.FuncMap {
return template.FuncMap{
"bgPatternCount": tm.bgPatternCount,
"debugMode": tm.debugMode,
"apiUrl": tm.apiURL,
}
}
func (tm *TemplateManager) bgPatternCount() uint8 {
return uint8(time.Now().UnixNano() % 17)
}
func (tm *TemplateManager) debugMode() bool {
return tm.debugModeEnabled
}
func (tm *TemplateManager) apiURL() string {
return tm.externalAPIEndpoint
}

View File

@@ -8,6 +8,37 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
func (wc *WebController) serveRegister(
w http.ResponseWriter,
r *http.Request,
p httprouter.Params,
) {
var tpld = wc.newTemplateData(w, r)
// This only runs on the first request
if wc.captchaSiteKey == "" {
var api = pixelapi.New(wc.conf.APIURLInternal, "")
capt, err := api.GetRecaptcha()
if err != nil {
log.Error("Error getting recaptcha key: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if capt.SiteKey == "" {
wc.captchaSiteKey = "none"
} else {
wc.captchaSiteKey = capt.SiteKey
}
}
tpld.Other = wc.captchaSiteKey
err := wc.templates.Get().ExecuteTemplate(w, "register", tpld)
if err != nil {
log.Error("Error executing template '%s': %s", "register", err)
}
}
func (wc *WebController) serveLogout( func (wc *WebController) serveLogout(
w http.ResponseWriter, w http.ResponseWriter,
r *http.Request, r *http.Request,

View File

@@ -7,7 +7,6 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"fornaxian.com/pixeldrain-web/init/conf" "fornaxian.com/pixeldrain-web/init/conf"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
"github.com/Fornaxian/log" "github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
@@ -16,8 +15,11 @@ import (
// proper context when running // proper context when running
type WebController struct { type WebController struct {
conf *conf.PixelWebConfig conf *conf.PixelWebConfig
templates *templates.TemplateManager templates *TemplateManager
staticResourceDir string staticResourceDir string
// page-specific variables
captchaSiteKey string
} }
// New initializes a new WebController by registering all the request handlers // New initializes a new WebController by registering all the request handlers
@@ -27,7 +29,7 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
conf: conf, conf: conf,
staticResourceDir: conf.StaticResourceDir, staticResourceDir: conf.StaticResourceDir,
} }
wc.templates = templates.New( wc.templates = NewTemplateManager(
conf.TemplateDir, conf.TemplateDir,
conf.APIURLExternal, conf.APIURLExternal,
conf.DebugMode, conf.DebugMode,
@@ -49,7 +51,7 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
r.GET(prefix+"/t" /* */, wc.serveTemplate("paste", false)) r.GET(prefix+"/t" /* */, wc.serveTemplate("paste", false))
// User account pages // User account pages
r.GET(prefix+"/register" /* */, wc.serveTemplate("register", false)) r.GET(prefix+"/register" /* */, wc.serveRegister)
r.GET(prefix+"/login" /* */, wc.serveTemplate("login", false)) r.GET(prefix+"/login" /* */, wc.serveTemplate("login", false))
r.GET(prefix+"/logout" /* */, wc.serveTemplate("logout", true)) r.GET(prefix+"/logout" /* */, wc.serveTemplate("logout", true))
r.POST(prefix+"/logout" /* */, wc.serveLogout) r.POST(prefix+"/logout" /* */, wc.serveLogout)