• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Managing Domain Account Plugins (for System Applications Only)
2
3<!--Kit: Basic Services Kit-->
4<!--Subsystem: Account-->
5<!--Owner: @steven-q-->
6<!--Designer: @JiDong-CS1-->
7<!--Tester: @zhaimengchao-->
8<!--Adviser: @zengyawen-->
9
10The system provides APIs for registering and unregistering a domain account plugin, which is used to customize domain account management.
11
12## Before You Start
13
141. Request the following permissions. For details, see [Requesting Permissions for system_basic Applications](../../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications).
15   - ohos.permission.MANAGE_LOCAL_ACCOUNTS
16   - ohos.permission.GET_DOMAIN_ACCOUNTS
17
182. Import the **osAccount** module.
19
20   ```ts
21   import { osAccount, AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
22   ```
23
243. Obtain an **AccountManager** instance.
25
26   ```ts
27   let accountMgr = osAccount.getAccountManager()
28   ```
29
30## Registering a Domain Account Plugin
31
32The domain account plugin prototype is [DomainPlugin](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#domainplugin9). The domain account plugin must inherit and implement the **DomainPlugin** APIs. You can use [registerPlugin](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#registerplugin9) to register a plugin.
33
34**Procedure**
35
361. Define the plugin.
37
38   ```ts
39   let plugin: osAccount.DomainPlugin = {
40     auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array,
41            callback: osAccount.IUserAuthCallback) => {
42       console.info("plugin auth domain" + domainAccountInfo.domain)
43       console.info("plugin auth accountName" + domainAccountInfo.accountName)
44       console.info("plugin auth accountId" + domainAccountInfo.accountId)
45
46       let result: osAccount.AuthResult = {
47         token: new Uint8Array([0]),
48         remainTimes: 5,
49         freezingTime: 0
50       };
51       callback.onResult(0, result);
52     },
53     authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo,
54                     callback: osAccount.IUserAuthCallback) => {
55       console.info("plugin authWithPopup domain" + domainAccountInfo.domain)
56       console.info("plugin authWithPopup accountName" + domainAccountInfo.accountName)
57       console.info("plugin authWithPopup accountId" + domainAccountInfo.accountId)
58
59       let result: osAccount.AuthResult = {
60         token: new Uint8Array([0]),
61         remainTimes: 5,
62         freezingTime: 0
63       };
64       callback.onResult(0, result);
65     },
66     authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array, callback: osAccount.IUserAuthCallback) => {
67       console.info("plugin authWithToken domain" + domainAccountInfo.domain)
68       console.info("plugin authWithToken accountName" + domainAccountInfo.accountName)
69       console.info("plugin authWithToken accountId" + domainAccountInfo.accountId)
70       let result: osAccount.AuthResult = {
71         token: new Uint8Array([0]),
72         remainTimes: 5,
73         freezingTime: 0
74       };
75       callback.onResult(0, result);
76     },
77     getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions,
78                      callback: AsyncCallback<osAccount.DomainAccountInfo>) => {
79       console.info("plugin getAccountInfo domain")
80       let domainAccountId = Date.now().toString()
81       let code: BusinessError = {
82         code: 0,
83         name: "mock_name",
84         message: "mock_message"
85       };
86       let domainStr: string = '';
87       if (options.domain != undefined) {
88        domainStr = options.domain
89       }
90       let accountInfo: osAccount.DomainAccountInfo = {
91         domain: domainStr,
92         accountName: options.accountName,
93         accountId: domainAccountId,
94         isAuthenticated: false
95       };
96       callback(code, accountInfo);
97     },
98     getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo,
99                         callback: AsyncCallback<osAccount.AuthStatusInfo>) => {
100
101       console.info("plugin getAuthStatusInfo domain" + domainAccountInfo.domain)
102       console.info("plugin getAuthStatusInfo accountName" + domainAccountInfo.accountName)
103       console.info("plugin getAuthStatusInfo accountId" + domainAccountInfo.accountId)
104
105       let code: BusinessError = {
106         code: 0,
107         name: "mock_name",
108         message: "mock_message"
109       };
110       let statusInfo: osAccount.AuthStatusInfo = {
111         remainTimes: 5,
112         freezingTime: 0
113       };
114       callback(code, statusInfo);
115     },
116     bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number,
117                   callback: AsyncCallback<void>) => {
118       console.info("plugin bindAccount domain" + domainAccountInfo.domain)
119       console.info("plugin bindAccount accountName" + domainAccountInfo.accountName)
120       console.info("plugin bindAccount accountId" + domainAccountInfo.accountId)
121       let code: BusinessError = {
122         code: 0,
123         name: "mock_name",
124         message: "mock_message"
125       };
126       callback(code);
127     },
128     unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => {
129       console.info("plugin unbindAccount domain" + domainAccountInfo.domain)
130       console.info("plugin unbindAccount accountName" + domainAccountInfo.accountName)
131       console.info("plugin unbindAccount accountId" + domainAccountInfo.accountId)
132     },
133     isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array,
134                           callback: AsyncCallback<boolean>) => {
135       console.info("plugin isAccountTokenValid domain" + domainAccountInfo.domain)
136       console.info("plugin isAccountTokenValid accountName" + domainAccountInfo.accountName)
137       console.info("plugin isAccountTokenValid accountId" + domainAccountInfo.accountId)
138       let code: BusinessError = {
139         code: 0,
140         name: "mock_name",
141         message: "mock_message"
142       };
143       callback(code, true);
144     },
145     getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => {
146       console.info("plugin getAccessToken domain")
147       let code: BusinessError = {
148         code: 0,
149         name: "mock_name",
150         message: "mock_message"
151       };
152       let token: Uint8Array = new Uint8Array([0]);
153       callback(code, token);
154     }
155   }
156   ```
157
1582. Use [registerPlugin](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#registerplugin9) to register the plug-in.
159
160   ```ts
161   try {
162       osAccount.DomainAccountManager.registerPlugin(plugin)
163       console.info("registerPlugin success")
164   } catch (err) {
165       console.error("registerPlugin err: " + JSON.stringify(err));
166   }
167   ```
168
169## Unregistering a Domain Account Plugin
170
171Use [unregisterPlugin](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#unregisterplugin9) to unregister a domain account plugin that is not required.
172
173**Procedure**
174
175```ts
176try {
177  osAccount.DomainAccountManager.unregisterPlugin();
178  console.log('unregisterPlugin success.');
179} catch(err) {
180  console.error('unregisterPlugin err:' + JSON.stringify(err));
181}
182```
183