rework recovery email
This commit is contained in:
		
							
								
								
									
										21
									
								
								auth.go
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								auth.go
									
									
									
									
									
								
							@@ -3,6 +3,8 @@ package scsusers
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"golang.org/x/crypto/bcrypt"
 | 
			
		||||
)
 | 
			
		||||
@@ -13,8 +15,23 @@ func Login(username, password string) bool {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	if bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) != nil {
 | 
			
		||||
		log.Printf("scsusers.Login: Failed password for " + username)
 | 
			
		||||
		return false
 | 
			
		||||
		rc, ok := u.Get("recoverycode")
 | 
			
		||||
		if !ok || bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(rc)) != nil {
 | 
			
		||||
			log.Printf("scsusers.Login: Failed password for " + username)
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
		tmp, ok := u.Get("recoverytime")
 | 
			
		||||
		if !ok {
 | 
			
		||||
			log.Printf("scsusers.Login: recoverytime missing " + username)
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
		rt, _ := strconv.ParseInt(tmp, 10, 64)
 | 
			
		||||
		if time.Now().Unix() > rt {
 | 
			
		||||
			log.Printf("scsusers.Login: recovery time expired")
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
		u.Delete("recoverykey")
 | 
			
		||||
		u.Delete("recoverytime")
 | 
			
		||||
	}
 | 
			
		||||
	log.Printf("User %s logged in\n", username)
 | 
			
		||||
	return true
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,7 @@ func RecoverByEmail(email string) {
 | 
			
		||||
	recoverycode := randBytes(16)
 | 
			
		||||
 | 
			
		||||
	u.Set("recoverycode", string(recoverycode))
 | 
			
		||||
	u.Set("recoverytime", fmt.Sprintf("%d", time.Now().Unix()))
 | 
			
		||||
	u.Set("recoverytime", fmt.Sprintf("%d", time.Now().Add(time.Minute*60).Unix()))
 | 
			
		||||
 | 
			
		||||
	SendRecoveryEmail(email, email, string(recoverycode))
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user