mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Initial commit Signed-off-by: b1ackd0t <blackd0t@protonmail.com> * Initial commit Signed-off-by: b1ackd0t <blackd0t@protonmail.com> * Fix CI Signed-off-by: b1ackd0t <blackd0t@protonmail.com> * fix tests: add enabled status key Signed-off-by: rodneyosodo <socials@rodneyosodo.com> * start with token Signed-off-by: rodneyosodo <socials@rodneyosodo.com> Signed-off-by: b1ackd0t <blackd0t@protonmail.com> Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> Signed-off-by: rodneyosodo <socials@rodneyosodo.com> Co-authored-by: rodneyosodo <socials@rodneyosodo.com>
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package sdk
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
)
|
|
|
|
type keyReq struct {
|
|
Type uint32 `json:"type,omitempty"`
|
|
Duration time.Duration `json:"duration,omitempty"`
|
|
}
|
|
|
|
const keysEndpoint = "keys"
|
|
|
|
const (
|
|
// LoginKey is temporary User key received on successfull login.
|
|
LoginKey uint32 = iota
|
|
// RecoveryKey represents a key for resseting password.
|
|
RecoveryKey
|
|
// APIKey enables the one to act on behalf of the user.
|
|
APIKey
|
|
)
|
|
|
|
func (sdk mfSDK) Issue(d time.Duration, token string) (KeyRes, errors.SDKError) {
|
|
datareq := keyReq{Type: APIKey, Duration: d}
|
|
data, err := json.Marshal(datareq)
|
|
if err != nil {
|
|
return KeyRes{}, errors.NewSDKError(err)
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s", sdk.authURL, keysEndpoint)
|
|
|
|
_, body, sdkerr := sdk.processRequest(http.MethodPost, url, token, string(CTJSON), data, http.StatusCreated)
|
|
if sdkerr != nil {
|
|
return KeyRes{}, sdkerr
|
|
}
|
|
|
|
var key KeyRes
|
|
if err := json.Unmarshal(body, &key); err != nil {
|
|
return KeyRes{}, errors.NewSDKError(err)
|
|
}
|
|
|
|
return key, nil
|
|
}
|
|
|
|
func (sdk mfSDK) Revoke(id, token string) errors.SDKError {
|
|
url := fmt.Sprintf("%s/%s/%s", sdk.authURL, keysEndpoint, id)
|
|
_, _, err := sdk.processRequest(http.MethodDelete, url, token, string(CTJSON), nil, http.StatusNoContent)
|
|
return err
|
|
}
|
|
|
|
func (sdk mfSDK) RetrieveKey(id, token string) (retrieveKeyRes, errors.SDKError) {
|
|
url := fmt.Sprintf("%s/%s/%s", sdk.authURL, keysEndpoint, id)
|
|
_, body, err := sdk.processRequest(http.MethodGet, url, token, string(CTJSON), nil, http.StatusOK)
|
|
if err != nil {
|
|
return retrieveKeyRes{}, err
|
|
}
|
|
|
|
var key retrieveKeyRes
|
|
if err := json.Unmarshal(body, &key); err != nil {
|
|
return retrieveKeyRes{}, errors.NewSDKError(err)
|
|
}
|
|
|
|
return key, nil
|
|
}
|