示例#1
0
文件: auth.go 项目: Monory/messenger
func MakeChatToken(db *sql.DB, t *UserToken) (*UserToken, error) {
	result := NewUserToken()
	for selectorExists := true; selectorExists; {
		result.Random()
		var err error
		selectorExists, err = database.CheckChatSelectorExists(db, result.Selector)
		if err != nil {
			return result, err
		}
	}

	dbToken, err := database.GetToken(db, t.Selector)
	if err != nil {
		return result, err
	}

	chatDBToken := result.DBToken()
	chatDBToken.UserID = dbToken.UserID
	err = database.AddChatToken(db, chatDBToken)
	if err != nil {
		return result, err
	}

	return result, nil
}
示例#2
0
文件: auth.go 项目: Monory/messenger
func CheckUserToken(db *sql.DB, t *UserToken) error {
	dbToken, err := database.GetToken(db, t.Selector)
	if err != nil {
		if err == sql.ErrNoRows {
			return ErrInvalidToken
		}
		return err
	}

	hash := sha256.Sum256(t.Validator)
	if subtle.ConstantTimeCompare(hash[:], dbToken.Token) == 1 {
		return nil
	}

	return ErrInvalidToken
}