This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from msoedov/restore_session
Restore previous session
- Loading branch information
Showing
6 changed files
with
110 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ src/ | |
slides/ | ||
main | ||
node_modules | ||
hacker-slides |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package auth | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func Header(c *gin.Context, key string) string { | ||
if values, _ := c.Request.Header[key]; len(values) > 0 { | ||
return values[0] | ||
} | ||
return "" | ||
} | ||
|
||
func BasicAuth() gin.HandlerFunc { | ||
realm := "Authorization Required" | ||
realm = "Basic realm=" + strconv.Quote(realm) | ||
user := os.Getenv("USER") | ||
password := os.Getenv("PASSWORD") | ||
enabled := isEnabled(user, password) | ||
if enabled { | ||
log.Warn("Auth mode enabled") | ||
log.Warn(fmt.Sprintf("Visit http://%s:%s@0.0.0.0:8080", user, password)) | ||
} | ||
return func(c *gin.Context) { | ||
header := Header(c, "Authorization") | ||
if enabled && header != authorizationHeader(user, password) { | ||
// Credentials doesn't match, we return 401 and abort handlers chain. | ||
c.Header("WWW-Authenticate", realm) | ||
c.AbortWithStatus(401) | ||
return | ||
} | ||
c.Next() | ||
} | ||
} | ||
|
||
func isEnabled(user, password string) bool { | ||
switch { | ||
case user == "": | ||
return false | ||
case password == "": | ||
return false | ||
default: | ||
return true | ||
} | ||
} | ||
|
||
func authorizationHeader(user, password string) string { | ||
base := user + ":" + password | ||
return "Basic " + base64.StdEncoding.EncodeToString([]byte(base)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package files | ||
|
||
import ( | ||
"io/ioutil" | ||
"time" | ||
) | ||
|
||
var epoch = time.Unix(1494505756, 0) | ||
|
||
func LatestFileIn(path string) (latest string) { | ||
files, err := ioutil.ReadDir(path) | ||
if err != nil { | ||
return "" | ||
} | ||
latestTime := epoch | ||
for _, f := range files { | ||
path := f.Name() | ||
pathModifiedAt := f.ModTime() | ||
if pathModifiedAt.After(latestTime) { | ||
latestTime = pathModifiedAt | ||
latest = path | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters