-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7856097
commit 67e1a9f
Showing
4 changed files
with
161 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (c) 2024 plgd.dev s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"), | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
****************************************************************************/ | ||
|
||
package cloud | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/plgd-dev/device/v2/pkg/codec/cbor" | ||
"github.com/plgd-dev/device/v2/schema/cloud" | ||
"github.com/plgd-dev/go-coap/v3/message" | ||
"github.com/plgd-dev/go-coap/v3/message/codes" | ||
"github.com/plgd-dev/go-coap/v3/message/pool" | ||
) | ||
|
||
type CoapSignUpRequest struct { | ||
DeviceID string `json:"di"` | ||
AuthorizationCode string `json:"accesstoken"` | ||
AuthorizationProvider string `json:"authprovider"` | ||
} | ||
|
||
type CoapSignUpResponse struct { | ||
AccessToken string `yaml:"accessToken" json:"accesstoken"` | ||
UserID string `yaml:"userID" json:"uid"` | ||
RefreshToken string `yaml:"refreshToken" json:"refreshtoken"` | ||
RedirectURI string `yaml:"-" json:"redirecturi"` | ||
ExpiresIn int64 `yaml:"-" json:"expiresin"` | ||
ValidUntil time.Time `yaml:"-" jsom:"-"` | ||
} | ||
|
||
var ( | ||
ErrMissingAuthorizationCode = fmt.Errorf("authorization code missing") | ||
ErrMissingAuthorizationProvider = fmt.Errorf("authorization provider missing") | ||
ErrCannotSignUp = fmt.Errorf("cannot sign up") | ||
) | ||
|
||
func MakeSignUpRequest(deviceID, code, provider string) (CoapSignUpRequest, error) { | ||
if code == "" { | ||
return CoapSignUpRequest{}, ErrMissingAuthorizationCode | ||
} | ||
if provider == "" { | ||
return CoapSignUpRequest{}, ErrMissingAuthorizationProvider | ||
} | ||
|
||
return CoapSignUpRequest{ | ||
DeviceID: deviceID, | ||
AuthorizationCode: code, | ||
AuthorizationProvider: provider, | ||
}, nil | ||
} | ||
|
||
func setSignUpRequest(req *pool.Message, cfg Configuration, deviceID string) error { | ||
signUpRequest, err := MakeSignUpRequest(deviceID, cfg.AuthorizationCode, cfg.AuthorizationProvider) | ||
if err != nil { | ||
return err | ||
} | ||
inputCbor, err := cbor.Encode(signUpRequest) | ||
if err != nil { | ||
return err | ||
} | ||
req.SetCode(codes.POST) | ||
token, err := message.GetToken() | ||
if err != nil { | ||
return err | ||
} | ||
req.SetToken(token) | ||
if err = req.SetPath(SignUp); err != nil { | ||
return err | ||
} | ||
req.SetContentFormat(message.AppOcfCbor) | ||
req.SetBody(bytes.NewReader(inputCbor)) | ||
return nil | ||
} | ||
|
||
func (c *Manager) signUp(ctx context.Context) error { | ||
creds := c.getCreds() | ||
if creds.AccessToken != "" { | ||
return nil | ||
} | ||
req := c.client.AcquireMessage(ctx) | ||
err := setSignUpRequest(req, c.getCloudConfiguration(), c.deviceID.String()) | ||
if err != nil { | ||
return fmt.Errorf("%w: %w", ErrCannotSignUp, err) | ||
} | ||
c.setProvisioningStatus(cloud.ProvisioningStatus_REGISTERING) | ||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("%w: %w", ErrCannotSignUp, err) | ||
} | ||
if resp.Code() != codes.Changed { | ||
return fmt.Errorf("%w: unexpected status code %v", ErrCannotSignUp, resp.Code()) | ||
} | ||
var signUpResp CoapSignUpResponse | ||
err = cbor.ReadFrom(resp.Body(), &signUpResp) | ||
if err != nil { | ||
return fmt.Errorf("%w: %w", ErrCannotSignUp, err) | ||
} | ||
c.setCreds(signUpResp) | ||
log.Printf("signed up\n") | ||
c.save() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (c) 2024 plgd.dev s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"), | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
****************************************************************************/ | ||
|
||
package cloud_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/plgd-dev/device/v2/bridge/device/cloud" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMakeSignUpRequest(t *testing.T) { | ||
_, err := cloud.MakeSignUpRequest("id", "", "provider") | ||
require.ErrorIs(t, err, cloud.ErrMissingAuthorizationCode) | ||
|
||
_, err = cloud.MakeSignUpRequest("id", "code", "") | ||
require.ErrorIs(t, err, cloud.ErrMissingAuthorizationProvider) | ||
|
||
req, err := cloud.MakeSignUpRequest("id", "code", "provider") | ||
require.NoError(t, err) | ||
require.Equal(t, "id", req.DeviceID) | ||
require.Equal(t, "code", req.AuthorizationCode) | ||
require.Equal(t, "provider", req.AuthorizationProvider) | ||
} |