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
// Recaptcha stores the reCaptcha site key
type Recaptcha struct {
SiteKey string `json:"site_key"`
}
func (p *PixelAPI) GetRecaptcha() (resp *Recaptcha, err error) {
err = p.jsonRequest("GET", p.apiEndpoint+"/misc/recpatcha", resp)
// GetRecaptcha gets the reCaptcha site key from the pixelapi server. If
// 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 {
return nil, err
return resp, err
}
return resp, nil
}

View File

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

View File

@@ -204,6 +204,12 @@ var UploadWorker = /** @class */ (function () {
});
};
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");
// First upload in this browser
if (uc === null) {

View File

@@ -117,6 +117,13 @@ class UploadWorker {
}
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")
// First upload in this browser

View File

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

View File

@@ -1,9 +1,10 @@
package templates
package webcontroller
import (
"html/template"
"os"
"path/filepath"
"time"
"github.com/Fornaxian/log"
)
@@ -17,7 +18,7 @@ type TemplateManager struct {
debugModeEnabled bool
}
func New(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager {
func NewTemplateManager(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager {
return &TemplateManager{
templateDir: templateDir,
externalAPIEndpoint: externalAPIEndpoint,
@@ -65,3 +66,23 @@ func (tm *TemplateManager) Get() *template.Template {
}
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"
)
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(
w http.ResponseWriter,
r *http.Request,

View File

@@ -7,7 +7,6 @@ import (
"github.com/google/uuid"
"fornaxian.com/pixeldrain-web/init/conf"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
@@ -16,8 +15,11 @@ import (
// proper context when running
type WebController struct {
conf *conf.PixelWebConfig
templates *templates.TemplateManager
templates *TemplateManager
staticResourceDir string
// page-specific variables
captchaSiteKey string
}
// 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,
staticResourceDir: conf.StaticResourceDir,
}
wc.templates = templates.New(
wc.templates = NewTemplateManager(
conf.TemplateDir,
conf.APIURLExternal,
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))
// 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+"/logout" /* */, wc.serveTemplate("logout", true))
r.POST(prefix+"/logout" /* */, wc.serveLogout)