47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package gameplatform
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/sha1"
|
|
"crypto/subtle"
|
|
"encoding/hex"
|
|
"kra/internal/biz/business"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type Passwords struct{ AuthCode, MigrateAuthCode, TablePrefix string }
|
|
|
|
func NewPasswords() business.PlatformPasswordVerifier {
|
|
prefix := os.Getenv("SW_SDK_TABLE_PREFIX")
|
|
if prefix == "" {
|
|
prefix = "sys_"
|
|
}
|
|
return &Passwords{AuthCode: os.Getenv("SW_SDK_AUTH_CODE"), MigrateAuthCode: os.Getenv("SW_SDK_MIGRATE_AUTH_CODE"), TablePrefix: prefix}
|
|
}
|
|
func md5Hex(value string) string { sum := md5.Sum([]byte(value)); return hex.EncodeToString(sum[:]) }
|
|
|
|
func (p *Passwords) Verify(password, stored string) (bool, error) {
|
|
if strings.TrimSpace(password) == "" || strings.TrimSpace(stored) == "" {
|
|
return false, nil
|
|
}
|
|
var expected string
|
|
switch {
|
|
case strings.HasPrefix(stored, "###"):
|
|
if p.AuthCode == "" {
|
|
return false, business.ErrPlatformPasswordConfig
|
|
}
|
|
expected = "###" + md5Hex(md5Hex(p.AuthCode+password))
|
|
case strings.HasPrefix(stored, "##$##"):
|
|
if p.MigrateAuthCode == "" {
|
|
return false, business.ErrPlatformPasswordConfig
|
|
}
|
|
sha := sha1.Sum([]byte(password))
|
|
expected = "##$##" + md5Hex(hex.EncodeToString(sha[:])+p.MigrateAuthCode)
|
|
default:
|
|
decor := md5Hex(p.TablePrefix)
|
|
expected = decor[:12] + md5Hex(password) + decor[len(decor)-4:]
|
|
}
|
|
return subtle.ConstantTimeCompare([]byte(expected), []byte(stored)) == 1, nil
|
|
}
|