Skip to content

Commit

Permalink
Added basic auth, todo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Wolf committed Apr 11, 2012
1 parent 3859231 commit 28c429b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
29 changes: 29 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Setup
===

# Get your [dev environment](https://developers.google.com/appengine/docs/go/gettingstarted/devenvironment) setup for GAE. It comes with a version of Go, so don't worry about compiling/installing that.
# Clone this repository, cd into the directory.
# $ cp app.yaml.example app.yaml
# Edit app.yaml with the app name you plan to use.

Running locally
===

# Start the dev server: $ dev_appserver.py .
# Seed the server with starting data: $ curl whatever:bees@localhost:8080/yes
# Visit localhost:8080 in your browser.

Your novelty server is ready to go. The answer is current set to "yes", and the
password for changing the answer is "bees".

To change the answer to "no", simply visit larry:bees@localhost:8080/no

Running on appspot
===

# Follow the [registration instuctions](https://developers.google.com/appengine/docs/go/gettingstarted/uploading) for GAE.
# Make sure that the app id in app.yaml matches you new app id.
# Push the app: $ appcfg.py .
# Seed the server with starting data. I'd suggest a different password than
"bees": $ curl moe:$PASSWORD@$APPID.appspot.com/yes
# Visit $APPID.appspot.com in your browser to behold your new novelty server.
7 changes: 1 addition & 6 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
* readme
* use post instead of get for editing
* basic auth for editing
* backend replication to survive instance restarts
* styling for index.template
* make sure we're sending right content types
* html error pages
* env variables for question, basic auth header
* salt password?
38 changes: 38 additions & 0 deletions novelty/novelty.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ package novelty
import (
"appengine"
"appengine/datastore"
"encoding/base64"
"html/template"
"net/http"
"strings"
)

type Answer struct {
Value string
}

type Password struct {
Value string
}

func init() {
http.HandleFunc("/", getAnswer)
http.HandleFunc("/yes", setAnswer("yes"))
Expand All @@ -31,8 +37,40 @@ func getAnswer(w http.ResponseWriter, r *http.Request) {
}
}

func authorized(r *http.Request) bool {
h := r.Header.Get("Authorization")
if !strings.HasPrefix(h, "Basic ") {
return false
}
a, _ := base64.StdEncoding.DecodeString(strings.TrimLeft(h, "Basic "))
fs := strings.Split(string(a), ":")
if len(fs) != 2 {
return false
}
c := appengine.NewContext(r)
k := datastore.NewKey(c, "Password", "password", 0, nil)
p := new(Password)
if err := datastore.Get(c, k, p); err != nil {
// If password is not set, seed with whatever password was passed in.
// See: http://golang.org/misc/dashboard/app/build/key.go
dp := Password{
Value: fs[1],
}
if _, err := datastore.Put(c, k, &dp); err != nil {
return false
}
return true
}
return p.Value == fs[1]
}

func setAnswer(answer string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if !authorized(r) {
w.Header().Set("WWW-Authenticate", "Basic realm=\"novelty.go\"")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
c := appengine.NewContext(r)
k := datastore.NewKey(c, "Answer", "answer", 0, nil)
a := Answer{
Expand Down

0 comments on commit 28c429b

Please sign in to comment.