Initial commit
This commit is contained in:
commit
f073e69417
28 changed files with 2564 additions and 0 deletions
75
auth.go
Normal file
75
auth.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/auth", apiAuthHandler(validateAuthToken))
|
||||
router.POST("/api/auth/logout", apiRawHandler(logout))
|
||||
}
|
||||
|
||||
func validateAuthToken(u *User, _ httprouter.Params, _ []byte) HTTPResponse {
|
||||
return APIResponse{u}
|
||||
}
|
||||
|
||||
func logout(w http.ResponseWriter, ps httprouter.Params, body []byte) HTTPResponse {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "auth",
|
||||
Value: "",
|
||||
Path: baseURL + "/",
|
||||
Expires: time.Unix(0,0),
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
})
|
||||
|
||||
return APIResponse{true}
|
||||
}
|
||||
|
||||
func completeAuth(w http.ResponseWriter, username string, email string, firstname string, lastname string, session *Session) (err error) {
|
||||
var usr User
|
||||
if !userExists(username) {
|
||||
if usr, err = NewUser(username, email, firstname, lastname); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if usr, err = getUserByLogin(username); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if session == nil {
|
||||
var s Session
|
||||
s, err = usr.NewSession()
|
||||
session = &s
|
||||
} else {
|
||||
_, err = session.SetUser(usr)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "auth",
|
||||
Value: base64.StdEncoding.EncodeToString(session.Id),
|
||||
Path: baseURL + "/",
|
||||
Expires: time.Now().Add(30 * 24 * time.Hour),
|
||||
//Secure: true,
|
||||
HttpOnly: true,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func dummyAuth(w http.ResponseWriter, _ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var lf map[string]string
|
||||
if err := json.Unmarshal(body, &lf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return map[string]string{"status": "OK"}, completeAuth(w, lf["login"], lf["email"], lf["firstname"], lf["lastname"], nil)
|
||||
}
|
Reference in a new issue