pogo/auth/auth.go

91 lines
1.8 KiB
Go
Raw Normal View History

2017-10-03 12:08:27 +01:00
package auth
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"log"
2017-10-03 12:08:27 +01:00
"net/http"
"strings"
2017-10-03 12:08:27 +01:00
"github.com/ishanjain28/pogo/common"
)
func RequireAuthorization() common.Handler {
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
if usr := decryptSession(r); usr != nil {
2017-10-03 12:08:27 +01:00
rc.User = usr
return nil
}
if strings.Contains(r.Header.Get("Accept"), "html") || r.Method == "GET" {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return nil
} else {
return &common.HTTPError{
Message: "Unauthorized!",
StatusCode: http.StatusUnauthorized,
}
}
2017-10-03 12:08:27 +01:00
return &common.HTTPError{
Message: "Unauthorized!",
StatusCode: http.StatusUnauthorized,
}
}
}
func CreateSession(u *common.User, w http.ResponseWriter) error {
// n_J6vaKjmmw4WB95DMorjQ.UMYdBLfttwPgQw9T0u0wdK7bGwDT9vwxoPAKWhjSAcpoiMsjh4eSfBkA4WB2deSoQu_cjCaJrcp77rvG67xkOeXsYpiclx2b-Oi7MHM3Kms.1507140277977.604800000.2CdxwiKAJT4SYJTVK-Du5jokr-CCnxo1ukdaVBkLRJg
iv, err := generateRandomString(16)
if err != nil {
return err
}
userJSON, err := json.Marshal(u)
if err != nil {
return err
}
var hexedJSON []byte
hex.Encode(hexedJSON, userJSON)
fmt.Println(iv, string(userJSON), hexedJSON)
block, err := aes.NewCipher(hexedJSON)
if err != nil {
return err
2017-10-03 12:08:27 +01:00
}
mode := cipher.NewCBCEncrypter(block, iv)
return nil
2017-10-03 12:08:27 +01:00
}
func decryptSession(r *http.Request) *common.User {
c, err := r.Cookie("POGO_SESSION")
if err != nil {
if err != http.ErrNoCookie {
log.Printf("error in reading Cookie: %v", err)
}
return nil
}
fmt.Println(c)
2017-10-03 12:08:27 +01:00
return nil
}
func generateRandomString(l int) ([]byte, error) {
rBytes := make([]byte, l)
_, err := rand.Read(rBytes)
if err != nil {
return nil, err
}
return rBytes, nil
}