Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseto committed Jun 2, 2015
2 parents 289bf67 + 8b2f3f7 commit 530242b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# fakecas
# Fake CAS

Download the binary from [here](https://github.com/CenterForOpenScience/fakecas/releases/download/0.2.0/fakecas)

```bash
cd ~/Downloads # cd to where you downloaded the file to
chmod +x fakecas # Make the server executable
./fakecas # Run the server

./fakecas -h # Print possible configuration options
# Usage of ./fakecas:
# -dbaddress="localhost:27017": The address of your mongodb. ie: localhost:27017
# -dbname="osf20130903": The name of your OSF database
# -host="localhost:8080": The host to bind to
```
31 changes: 26 additions & 5 deletions fakecas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"log"
Expand Down Expand Up @@ -43,12 +45,28 @@ type ServiceResponse struct {
UserName string `xml:"cas:authenticationSuccess>cas:attributes>username"`
}

var (
host = flag.String("host", "localhost:8080", "The host to bind to")
databasename = flag.String("dbname", "osf20130903", "The name of your OSF database")
databaseaddress = flag.String("dbaddress", "localhost:27017", "The address of your mongodb. ie: localhost:27017")
)

func main() {
flag.Parse()

http.HandleFunc("/login", login)
http.HandleFunc("/logout", logout)
http.HandleFunc("/oauth2/profile", oauth)
http.HandleFunc("/p3/serviceValidate", serviceValidate)
http.ListenAndServe("localhost:8080", nil)

fmt.Println("Expecting database", *databasename, " to be running at", *databaseaddress)
fmt.Println("Listening on", *host)

err := http.ListenAndServe(*host, nil)

if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

func login(w http.ResponseWriter, r *http.Request) {
Expand All @@ -62,27 +80,30 @@ func login(w http.ResponseWriter, r *http.Request) {
query.Set("ticket", r.FormValue("username"))
redir.RawQuery = query.Encode()

fmt.Println("Logging in and redirecting to", redir)
http.Redirect(w, r, redir.String(), http.StatusFound)
}

func logout(w http.ResponseWriter, r *http.Request) {
fmt.Println("Logging out and redirecting to", r.FormValue("service"))
http.Redirect(w, r, r.FormValue("service"), http.StatusFound)
}

func serviceValidate(w http.ResponseWriter, r *http.Request) {

session, err := mgo.Dial("localhost:27017")
session, err := mgo.Dial(*databaseaddress)
if err != nil {
panic(err)
}
defer session.Close()

c := session.DB("osf20130903").C("user")
c := session.DB(*databasename).C("user")

result := User{}
err = c.Find(bson.M{"username": r.FormValue("ticket")}).One(&result)

if err != nil {
fmt.Println("User", r.FormValue("ticket"), "not found.")
http.NotFound(w, r)
return
}
Expand All @@ -109,13 +130,13 @@ func serviceValidate(w http.ResponseWriter, r *http.Request) {

func oauth(w http.ResponseWriter, r *http.Request) {

session, err := mgo.Dial("localhost:27017")
session, err := mgo.Dial(*databaseaddress)
if err != nil {
panic(err)
}
defer session.Close()

c := session.DB("osf20130903").C("user")
c := session.DB(*databasename).C("user")

result := User{}
err = c.Find(bson.M{"_id": strings.Replace(r.Header.Get("Authorization"), "Bearer ", "", 1)}).One(&result)
Expand Down

0 comments on commit 530242b

Please sign in to comment.