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