1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "dm_auth_context.h"
17
18 namespace OHOS {
19 namespace DistributedHardware {
20
GetDeviceId(DmAuthSide side)21 std::string DmAuthContext::GetDeviceId(DmAuthSide side)
22 {
23 const DmAccess &localAccess = (direction == DM_AUTH_SOURCE) ? accesser : accessee;
24 const DmAccess &remoteAccess = (direction == DM_AUTH_SOURCE) ? accessee : accesser;
25 return (side == DM_AUTH_LOCAL_SIDE) ? localAccess.deviceId : remoteAccess.deviceId;
26 }
27
GetUserId(DmAuthSide side)28 int32_t DmAuthContext::GetUserId(DmAuthSide side)
29 {
30 const DmAccess &localAccess = (direction == DM_AUTH_SOURCE) ? accesser : accessee;
31 const DmAccess &remoteAccess = (direction == DM_AUTH_SOURCE) ? accessee : accesser;
32 return (side == DM_AUTH_LOCAL_SIDE) ? localAccess.userId : remoteAccess.userId;
33 }
34
GetCredentialId(DmAuthSide side,DmAuthScope authorizedScope)35 std::string DmAuthContext::GetCredentialId(DmAuthSide side, DmAuthScope authorizedScope)
36 {
37 const DmAccess &localAccess = (direction == DM_AUTH_SOURCE) ? accesser : accessee;
38 const DmAccess &remoteAccess = (direction == DM_AUTH_SOURCE) ? accessee : accesser;
39 const DmAccess &access = (side == DM_AUTH_LOCAL_SIDE) ? localAccess : remoteAccess;
40 return (authorizedScope == DM_AUTH_SCOPE_LNN) ? access.lnnCredentialId : access.transmitCredentialId;
41 }
42
GetPublicKey(DmAuthSide side,DmAuthScope authorizedScope)43 std::string DmAuthContext::GetPublicKey(DmAuthSide side, DmAuthScope authorizedScope)
44 {
45 const DmAccess &localAccess = (direction == DM_AUTH_SOURCE) ? accesser : accessee;
46 const DmAccess &remoteAccess = (direction == DM_AUTH_SOURCE) ? accessee : accesser;
47 const DmAccess &access = (side == DM_AUTH_LOCAL_SIDE) ? localAccess : remoteAccess;
48 return (authorizedScope == DM_AUTH_SCOPE_LNN) ? access.lnnPublicKey : access.transmitPublicKey;
49 }
50
SetCredentialId(DmAuthSide side,DmAuthScope authorizedScope,const std::string & credentialId)51 void DmAuthContext::SetCredentialId(DmAuthSide side, DmAuthScope authorizedScope, const std::string &credentialId)
52 {
53 DmAccess &localAccess = (direction == DM_AUTH_SOURCE) ? accesser : accessee;
54 DmAccess &remoteAccess = (direction == DM_AUTH_SOURCE) ? accessee : accesser;
55 DmAccess &access = (side == DM_AUTH_LOCAL_SIDE) ? localAccess : remoteAccess;
56 std::string &credId = (authorizedScope == DM_AUTH_SCOPE_LNN) ?
57 access.lnnCredentialId : access.transmitCredentialId;
58 credId = credentialId;
59 return;
60 }
61
SetPublicKey(DmAuthSide side,DmAuthScope authorizedScope,const std::string & publicKey)62 void DmAuthContext::SetPublicKey(DmAuthSide side, DmAuthScope authorizedScope, const std::string &publicKey)
63 {
64 DmAccess &localAccess = (direction == DM_AUTH_SOURCE) ? accesser : accessee;
65 DmAccess &remoteAccess = (direction == DM_AUTH_SOURCE) ? accessee : accesser;
66 DmAccess &access = (side == DM_AUTH_LOCAL_SIDE) ? localAccess : remoteAccess;
67 std::string &key = (authorizedScope == DM_AUTH_SCOPE_LNN) ?
68 access.lnnPublicKey : access.transmitPublicKey;
69 key = publicKey;
70
71 return;
72 }
73
GetAccountId(DmAuthSide side)74 std::string DmAuthContext::GetAccountId(DmAuthSide side)
75 {
76 DmAccess &localAccess = (direction == DM_AUTH_SOURCE) ? accesser : accessee;
77 DmAccess &remoteAccess = (direction == DM_AUTH_SOURCE) ? accessee : accesser;
78 return (side == DM_AUTH_LOCAL_SIDE) ? localAccess.accountId : remoteAccess.accountId;
79 }
80 } // namespace DistributedHardware
81 } // namespace OHOS