1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 10:17:26 +08:00

Allow to override session timeout. (#86)

* Allow to override session timeout.

This comes useful as it can be overriden from the SDK now

* Fix comments
This commit is contained in:
Marcos Nils 2017-01-31 13:51:57 +02:00 committed by GitHub
parent 1d37f98efd
commit a5d1e5c207
6 changed files with 24 additions and 20 deletions

View File

@ -9,6 +9,7 @@ import (
) )
func NewSession(rw http.ResponseWriter, req *http.Request) { func NewSession(rw http.ResponseWriter, req *http.Request) {
req.ParseForm()
if !services.IsHuman(req) { if !services.IsHuman(req) {
// User it not a human // User it not a human
rw.WriteHeader(http.StatusConflict) rw.WriteHeader(http.StatusConflict)
@ -16,7 +17,10 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
return return
} }
s, err := services.NewSession() reqDur := req.Form.Get("session-duration")
duration := services.GetDuration(reqDur)
s, err := services.NewSession(duration)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
//TODO: Return some error code //TODO: Return some error code

View File

@ -35,7 +35,6 @@ func IsHuman(req *http.Request) bool {
if os.Getenv("GOOGLE_RECAPTCHA_DISABLED") != "" { if os.Getenv("GOOGLE_RECAPTCHA_DISABLED") != "" {
return true return true
} }
req.ParseForm()
challenge := req.Form.Get("g-recaptcha-response") challenge := req.Form.Get("g-recaptcha-response")
// Of X-Forwarded-For exists, it means we are behind a loadbalancer and we should use the real IP address of the user // Of X-Forwarded-For exists, it means we are behind a loadbalancer and we should use the real IP address of the user

View File

@ -8,7 +8,6 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -202,24 +201,26 @@ func CloseSession(s *Session) error {
return nil return nil
} }
// Todo: this handles minimum viable product and removes hard-coding of hours value :) var defaultDuration = 4 * time.Hour
// For future enhance to return time.Duration and parse a string / flag.
func getExpiryHours() int { func GetDuration(reqDur string) time.Duration {
hours := 4 if reqDur != "" {
override := os.Getenv("EXPIRY") if dur, err := time.ParseDuration(reqDur); err == nil {
if len(override) > 0 { return dur
value, err := strconv.Atoi(override)
if err == nil {
hours = value
} }
return defaultDuration
} }
return hours
envDur := os.Getenv("EXPIRY")
if dur, err := time.ParseDuration(envDur); err == nil {
return dur
}
return defaultDuration
} }
func NewSession() (*Session, error) { func NewSession(duration time.Duration) (*Session, error) {
hours := getExpiryHours()
duration := time.Duration(hours) * time.Hour
s := &Session{} s := &Session{}
s.Id = uuid.NewV4().String() s.Id = uuid.NewV4().String()
s.Instances = map[string]*Instance{} s.Instances = map[string]*Instance{}

View File

@ -7,8 +7,7 @@
// Controller keeps code/logic separate from the HTML // Controller keeps code/logic separate from the HTML
app.controller("BypassController", ['$scope', '$log', '$http', '$location', '$timeout', function($scope, $log, $http, $location, $timeout) { app.controller("BypassController", ['$scope', '$log', '$http', '$location', '$timeout', function($scope, $log, $http, $location, $timeout) {
setTimeout(function() { setTimeout(function() {
var el = document.querySelector("#submit"); document.getElementById("welcomeFormBypass").submit();
el.click();
}, 500); }, 500);
}]); }]);

View File

@ -10,7 +10,7 @@
<h1>Welcome!</h1> <h1>Welcome!</h1>
<h2>We're bypassing the Captcha and redirecting you now..</h2> <h2>We're bypassing the Captcha and redirecting you now..</h2>
<form id="welcomeFormBypass" method="POST" action="/"> <form id="welcomeFormBypass" method="POST" action="/">
<button id="submit" type="submit">Start Session</button> <button id="start" type="submit">Start Session</button>
</form> </form>
</div> </div>
</body> </body>

View File

@ -13,6 +13,7 @@
<h2>Before starting we need to verify you are a human</h2> <h2>Before starting we need to verify you are a human</h2>
<form id="welcomeForm" method="POST" action="/"> <form id="welcomeForm" method="POST" action="/">
<div class="g-recaptcha" data-callback="iAmHuman" data-sitekey="{{.}}"></div> <div class="g-recaptcha" data-callback="iAmHuman" data-sitekey="{{.}}"></div>
<input type="hidden" name="session-duration" value="1h"/>
</form> </form>
<img src="/assets/large_h.png" /> <img src="/assets/large_h.png" />
</div> </div>