Exemple #1
0
func PutUserInfo(userInfo *UserInfo) error {
	oldUserInfo, err := GetUserInfo(userInfo.Username)
	if err != nil && err.Error() != "user not exist" {
		return err
	}
	if oldUserInfo != nil {
		return errors.New("duplicate user")
	}
	err = db.Update(func(tx *bolt.Tx) error {
		userBucket, err := tx.CreateBucket([]byte(userInfo.Username))
		if err != nil {
			return err
		}
		err = database.AddKeyValue(userBucket, "uid", userInfo.UID)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(userBucket, "username", userInfo.Username)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(userBucket, "password", userInfo.EncryptedPassword)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(userBucket, "salt", userInfo.Salt)
		if err != nil {
			return err
		}
		return nil
	})
	return err
}
func PutClientInfo(clientInfo *ClientInfo) error {
	oldClientInfo, err := GetClientInfo(clientInfo.ClientUsername)
	if err != nil && err.Error() != "client not exist" {
		return err
	}
	if oldClientInfo != nil {
		return errors.New("duplicate client username")
	}
	err = db.Update(func(tx *bolt.Tx) error {
		clientBucket, err := tx.CreateBucket([]byte(clientInfo.ClientUsername))
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "client_username", clientInfo.ClientUsername)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "client_password", clientInfo.EncryptedPassword)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "owner_username", clientInfo.OwnerUsername)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "authorization_code", clientInfo.GrantAuthorizationCode)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "implicit", clientInfo.GrantImplicit)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "resource_owner_credential", clientInfo.GrantResourceOwner)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "client_credential", clientInfo.GrantClientCredentials)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "redirect_uri_author_code", clientInfo.RedirectURIAuthorCode)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "redirect_uri_implicit", clientInfo.RedirectURIImplicit)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "client_name", clientInfo.ClientName)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "description", clientInfo.Description)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "salt", clientInfo.Salt)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "create_date", clientInfo.CreateDate)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "update_date", clientInfo.UpdateDate)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "create_user", clientInfo.CreateUser)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "update_user", clientInfo.UpdateUser)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "create_ip", clientInfo.CreateIP)
		if err != nil {
			return err
		}
		err = database.AddKeyValue(clientBucket, "update_ip", clientInfo.UpdateIP)
		if err != nil {
			return err
		}
		return nil
	})
	return err
}