@ -5,6 +5,7 @@
package models
package models
import (
import (
"crypto/sha256"
"encoding/hex"
"encoding/hex"
"errors"
"errors"
"fmt"
"fmt"
@ -13,8 +14,6 @@ import (
"strings"
"strings"
"time"
"time"
"github.com/dchest/scrypt"
"github.com/gogits/git"
"github.com/gogits/git"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/base"
@ -62,6 +61,7 @@ type User struct {
IsActive bool
IsActive bool
IsAdmin bool
IsAdmin bool
Rands string ` xorm:"VARCHAR(10)" `
Rands string ` xorm:"VARCHAR(10)" `
Salt string ` xorm:"VARCHAR(10)" `
Created time . Time ` xorm:"created" `
Created time . Time ` xorm:"created" `
Updated time . Time ` xorm:"updated" `
Updated time . Time ` xorm:"updated" `
}
}
@ -89,10 +89,9 @@ func (user *User) NewGitSig() *git.Signature {
}
}
// EncodePasswd encodes password to safe format.
// EncodePasswd encodes password to safe format.
func ( user * User ) EncodePasswd ( ) error {
func ( user * User ) EncodePasswd ( ) {
newPasswd , err := scrypt . Key ( [ ] byte ( user . Passwd ) , [ ] byte ( base . SecretKey ) , 16384 , 8 , 1 , 64 )
newPasswd := base . PBKDF2 ( [ ] byte ( user . Passwd ) , [ ] byte ( user . Salt ) , 10000 , 50 , sha256 . New )
user . Passwd = fmt . Sprintf ( "%x" , newPasswd )
user . Passwd = fmt . Sprintf ( "%x" , newPasswd )
return err
}
}
// Member represents user is member of organization.
// Member represents user is member of organization.
@ -148,9 +147,9 @@ func RegisterUser(user *User) (*User, error) {
user . Avatar = base . EncodeMd5 ( user . Email )
user . Avatar = base . EncodeMd5 ( user . Email )
user . AvatarEmail = user . Email
user . AvatarEmail = user . Email
user . Rands = GetUserSalt ( )
user . Rands = GetUserSalt ( )
if err = user . EncodePasswd ( ) ; err != nil {
user . Salt = GetUserSalt ( )
return nil , err
user . EncodePasswd ( )
} else if _ , err = orm . Insert ( user ) ; err != nil {
if _ , err = orm . Insert ( user ) ; err != nil {
return nil , err
return nil , err
} else if err = os . MkdirAll ( UserPath ( user . Name ) , os . ModePerm ) ; err != nil {
} else if err = os . MkdirAll ( UserPath ( user . Name ) , os . ModePerm ) ; err != nil {
if _ , err := orm . Id ( user . Id ) . Delete ( & User { } ) ; err != nil {
if _ , err := orm . Id ( user . Id ) . Delete ( & User { } ) ; err != nil {
@ -384,18 +383,20 @@ func GetUserByEmail(email string) (*User, error) {
// LoginUserPlain validates user by raw user name and password.
// LoginUserPlain validates user by raw user name and password.
func LoginUserPlain ( name , passwd string ) ( * User , error ) {
func LoginUserPlain ( name , passwd string ) ( * User , error ) {
user := User { LowerName : strings . ToLower ( name ) , Passwd : passwd }
user := User { LowerName : strings . ToLower ( name ) }
if err := user . EncodePasswd ( ) ; err != nil {
return nil , err
}
has , err := orm . Get ( & user )
has , err := orm . Get ( & user )
if err != nil {
if err != nil {
return nil , err
return nil , err
} else if ! has {
} else if ! has {
err = ErrUserNotExist
return nil , ErrUserNotExist
}
newUser := & User { Passwd : passwd , Salt : user . Salt }
newUser . EncodePasswd ( )
if user . Passwd != newUser . Passwd {
return nil , ErrUserNotExist
}
}
return & user , err
return & user , nil
}
}
// Follow is connection request for receiving user notifycation.
// Follow is connection request for receiving user notifycation.