Skip to content

Commit

Permalink
Add: config
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXu committed Apr 16, 2016
1 parent 7d211d3 commit 7665692
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config_prod.json
19 changes: 8 additions & 11 deletions auth/authNamePasswordController.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,23 @@ func LoginUserWithEmail(c *gin.Context){
var user *UserAccount
user, err := getUserByEmail(loginJson.Email, c)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"authenticated":"false"})
abortWithError(c, http.StatusUnauthorized, "authentication fail")
}

passwordValidErr := safeComparePassword(user.Password, []byte(loginJson.Password))
if passwordValidErr != nil{
c.JSON(http.StatusUnauthorized, gin.H{"authenticated":"false"})
abortWithError(c, http.StatusUnauthorized, "authentication fail")
}

tokenString, tokenErr := GenerateToken(loginJson.Email)

if tokenErr != nil{
c.JSON(http.StatusUnauthorized, gin.H{"authenticated":"false"})
abortWithError(c, http.StatusUnauthorized, "authentication fail")
}

c.JSON(http.StatusOK, gin.H{"authenticated":"true","token":tokenString})

// if loginJson.Email == "xw" && loginJson.Password =="xw"{
// c.JSON(http.StatusOK, gin.H{"authenticated":"true"})
// }else{
// c.JSON(http.StatusUnauthorized, gin.H{"authenticated":"false"})
// }

}else{
c.JSON(http.StatusUnauthorized, gin.H{"error":"unauthorized"})
}
Expand All @@ -60,16 +56,17 @@ func SignUpWithEmail(c *gin.Context) {
if validateEmail(signUpJson.Email, c) && validatePassword(signUpJson.Password){
hash, hasherr := hashPassword(signUpJson.Password)
if hasherr != nil{
c.JSON(http.StatusBadRequest, gin.H{"error":"sign up fail"})
abortWithError(c, http.StatusBadRequest, "signup fail")
}
db := c.MustGet(mongodb.DBMiddlewareName).(*mgo.Database)
err := db.C(CollectionUserAccount).Insert(&UserAccount{
Id:signUpJson.Email,
Password:hash,
CreatedOn:int64(time.Now().Second()),
UpdatedOn:int64(time.Now().Second())})
UpdatedOn:int64(time.Now().Second()),
Active:false})
if err != nil{
c.JSON(http.StatusBadRequest, gin.H{"error":"sign up fail"})
abortWithError(c, http.StatusBadRequest, "signup fail")
}

// sent email
Expand Down
19 changes: 19 additions & 0 deletions auth/authUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"golang.org/x/crypto/bcrypt"
"time"
"golang.org/x/crypto/rand"
)

func getUserByEmail(userId string, c *gin.Context) (*UserAccount, error){
Expand Down Expand Up @@ -50,6 +52,23 @@ func validatePassword(password string) bool{
return true
}

func generateRandomToken() []byte{
b := make([]byte, 24)
rand.Read(b)
return b
}

func sendRegistrationConfirmationEmail(email string, userId string, c *gin.Context){
contex := c.Copy()
randomToken := generateRandomToken()
db := context.MustGet(mongodb.DBMiddlewareName).(*mgo.Database)
err := db.C(CollectionEmailConfirmation).Insert(&emailConfirmation{
UserId:signUpJson.Email,
Token:randomToken,
Used: false,
ExpireAt:time.Now()})
}

func abortWithError(c *gin.Context, code int, message string) {
c.Header("WWW-Authenticate", "JWT realm="+Realm)
c.JSON(code, gin.H{
Expand Down
32 changes: 32 additions & 0 deletions auth/modelOperation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package auth

import (
"time"
)

const (
CollectionEmailConfirmation string = "email_confirmations_tmp"
CollectionPasswordChange string = "password_change_tmp"
)

const (
emailConfirmExpireTime time.Duration = time.Hour*7
passwordChangeExpireTime time.Duration = time.Hour*1
)


type tempOpsToken struct{
UserId string
Token []byte
Used bool
ExpireAt int64
}


type emailConfirmation struct{
tempOpsToken
}

type passwordChange struct{
tempOpsToken
}
1 change: 1 addition & 0 deletions auth/modelUser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type UserAccount struct{
SocialAuth SocialAuth
CreatedOn int64
UpdatedOn int64
Active bool
}

type UserProfile struct{
Expand Down
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"emails":[
{"address":"[email protected]", "password":"testing"},
{"address":"[email protected]", "password":"testing"}
],
"jwtkey":"testing"
}
30 changes: 30 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

import (
"os"
"encoding/json"
)

type Configuration struct{
emails []email
jwtkey string
}

type email struct{
address string
password string
}

func GetConfig() Configuration{
file,err:=os.Open("config.json")
if err != nil{
panic("Configuration file missing!")
}
decoder := json.NewDecoder(file)
config := Configuration{}
decodeErr := decoder.Decode(config)
if decoder!=nil {
panic("Configuration file error")
}
return config
}

0 comments on commit 7665692

Please sign in to comment.