openharmony 鸿蒙 js-apis-net-eap

2026-08-25 浏览 (1)

@ohos.net.eap (Extensible Authentication)

The eap module provides the extensible authentication mechanism to enable third-party clients to access custom 802.1X (a port-based network access control protocol) authentication, such as Extensible Authentication Protocol (EAP) authentication.

NOTE

The initial APIs of this module are supported since API version 20. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Module to Import

import {eap} from '@kit.NetworkKit';

eap.regCustomEapHandler

regCustomEapHandler(netType: number, eapCode: number, eapType: number, callback: Callback<EapData>): void

Registers a custom handler of Extensible Authentication Protocol (EAP) packets for extensible authentication. This API returns the result asynchronously through a callback.

The system will encapsulate the eligible EAP packets into the callback function for enterprise applications to retrieve.

Required permissions: ohos.permission.MANAGE_ENTERPRISE_WIFI_CONNECTION

System capability: SystemCapability.Communication.NetManager.Eap

Parameters

NameTypeMandatoryDescription
netTypenumberYesNetwork type. The value can be 1 or 2.
The value 1 indicates WLAN, and the value 2 indicates Ethernet.
eapCodenumberYesEAP code. The value can be any of the following:
code=1 Request, code=2 Response, code=3 Success, code=4 Failure.
eapTypenumberYesEAP method. The value range is [0, 255].
Common values include the following: eapType=1 Identity, eapType=2 Notification, eapType=3 NAK, eapType=4 MD5-Challenge, eapType=5 OTP (One-Time Password), eapType=6 GTC (Generic Token Card), eapType=13 EAP-TLS, eapType=21 EAP-TTLS, eapType=25 EAP-PEAP, eapType=254 Expanded Types, and eapType=255 Experimental use.
callbackCallback<EapData>YesCallback function, which returns the packet of the specified eapCode+eapType.

Error codes

For details about the error codes, see Universal Error Codes and Extensible Authentication Error Codes.

Error CodeError Message
201Permission denied.
33200006Invalid net type.
33200007Invalid eap code.
33200008Invalid eap type.
33200009netmanager stop.
33200099internal error.

Example

import {eap} from '@kit.NetworkKit';
let netType = 1;
let eapCode = 1;
let eapType = 25;
let  eapData = (eapData:eap.EapData):void => {
  console.info("rsp result",JSON.stringify(eapData))
}
    
try {
  eap.regCustomEapHandler(netType, eapCode, eapType, eapData);
  console.info('regCustomEapHandler success');
} catch (err) {
  console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

eap.unregCustomEapHandler

unregCustomEapHandler(netType:number, eapCode: number, eapType: number, callback: Callback<EapData>): void

Unregisters the custom handler of EAP packets for extensible authentication. This API returns the result asynchronously through a callback.

Required permissions: ohos.permission.MANAGE_ENTERPRISE_WIFI_CONNECTION

System capability: SystemCapability.Communication.NetManager.Eap

Parameters

NameTypeMandatoryDescription
netTypenumberYesNetwork type. The value can be 1 or 2.
The value 1 indicates WLAN, and the value 2 indicates Ethernet.
eapCodenumberYesEAP code. The value can be any of the following:
code=1 Request, code=2 Response, code=3 Success, code=4 Failure.
eapTypenumberYesEAP method. The value range is [0, 255].
Common values include the following: eapType=1 Identity, eapType=2 Notification, eapType=3 NAK, eapType=4 MD5-Challenge, eapType=5 OTP (One-Time Password), eapType=6 GTC (Generic Token Card), eapType=13 EAP-TLS, eapType=21 EAP-TTLS, eapType=25 EAP-PEAP, eapType=254 Expanded Types, and eapType=255 Experimental use.
callbackCallback<EapData>YesCallback function, which returns the packet of the specified eapCode+eapType.

Error codes

For details about the error codes, see Universal Error Codes and Extensible Authentication Error Codes.

Error CodeError Message
201Permission denied.
33200006Invalid net type.
33200007Invalid eap code.
33200008Invalid eap type.
33200009netmanager stop.
33200099internal error.

Example

import {eap} from '@kit.NetworkKit';
let netType = 1;
let eapCode = 1;
let eapType = 25;
let  eapData = (eapData:eap.EapData):void => {
  console.info("rsp result",JSON.stringify(eapData))
}
    
try {
  eap.unregCustomEapHandler(netType, eapCode, eapType, eapData);
  console.info('unregCustomEapHandler success');
} catch (err) {
  console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

eap.replyCustomEapData

replyCustomEapData(result: CustomResult, data: EapData): void

Notifies the system of the extensible authentication result.

NOTE

  • If this callback is used to process received EAP data packets, the customized portion added by the server must be removed from the EAP data transmitted to the system.
  • If this callback is used to process sent EAP data packets, the EAP data transmitted to the system is the EAP data with the customized portion added by the server.

Required permissions: ohos.permission.MANAGE_ENTERPRISE_WIFI_CONNECTION

System capability: SystemCapability.Communication.NetManager.Eap

Parameters

NameTypeMandatoryDescription
resultCustomResultYesExtensible authentication result.
dataEapDataYesEAP data.

Error codes

For details about the error codes, see Universal Error Codes and Extensible Authentication Error Codes.

Error CodeError Message
201Permission denied.
33200004Invalid result.
33200005Invalid size of eap data.
33200009netmanager stop.
33200099internal error.
import {eap} from '@kit.NetworkKit';
let eapData:eap.EapData= {
  msgId: 1,
  eapBuffer: new Uint8Array([1, 2, 3, 4, 5]),
  bufferLen: 5,
};
let result = 1;

try {
  eap.replyCustomEapData(result, eapData);
  console.info('replyCustomEapData success');
} catch (err) {
  console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

eap.startEthEap

startEthEap(netId: number, profile: EthEapProfile): void

Starts EAP authentication on an Ethernet NIC.

Required permissions: ohos.permission.MANAGE_ENTERPRISE_WIFI_CONNECTION

System capability: SystemCapability.Communication.NetManager.Eap

Parameters

NameTypeMandatoryDescription
netIdnumberYesID of the Ethernet NIC. If the default value -1 is specified, the system automatically matches the Ethernet NIC to initiate EAP authentication.
profileEthEapProfileYesEAP profile.

Error codes

For details about the error codes, see Universal Error Codes and Extensible Authentication Error Codes.

Error CodeError Message
201Permission denied.
33200001Invalid netId.
33200003Invalid profile.
33200009netmanager stop.
33200010invalid eth state.
33200099internal error.

Example

import {eap} from '@kit.NetworkKit';
let netId = 100;
let profile: eap.EthEapProfile = {
  eapMethod: eap.EapMethod.EAP_TTLS,
  phase2Method: eap.Phase2Method.PHASE2_AKA_PRIME,
  identity: "identity",
  anonymousIdentity: "anonymousIdentity",
  password: "password",
  caCertAliases: "caCertAliases",
  caPath: "caPath",
  clientCertAliases: "clientCertAliases",
  certEntry: new Uint8Array([5,6,7,8,9,10]),
  certPassword: "certPassword",
  altSubjectMatch: "altSubjectMatch",
  domainSuffixMatch: "domainSuffixMatch",
  realm: "realm",
  plmn: "plmn",
  eapSubId: 1
};
    
try {
  eap.startEthEap(netId, profile);
  console.info('startEthEap success');
} catch (err) {
  console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

eap.logOffEthEap

logOffEthEap(netId: number): void

Revokes the EAP-authenticated state of an Ethernet NIC.

Required permissions: ohos.permission.MANAGE_ENTERPRISE_WIFI_CONNECTION

System capability: SystemCapability.Communication.NetManager.Eap

Parameters

NameTypeMandatoryDescription
netIdnumberYesID of the Ethernet NIC. If the default value -1 is specified, the system automatically matches the Ethernet NIC to initiate EAP authentication.

Error codes

For details about the error codes, see Universal Error Codes and Extensible Authentication Error Codes.

Error CodeError Message
201Permission denied.
33200001Invalid netId.
33200002Log off fail.
33200009netmanager stop.
33200010invalid eth state.
33200099internal error.

Example

import {eap} from '@kit.NetworkKit';
let netId = 100;    
try{
  eap.logOffEthEap(netId);
  console.info("logOffEthEap success");
} catch (err) {
  console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

EapData

Defines the EAP data.

System capability: SystemCapability.Communication.NetManager.Eap

NameTypeRead-OnlyOptionalDescription
msgIdnumberNoNoPseudo random number used to associate the EAP data before and after processing.
eapBufferUint8ArrayNoNoRaw EAP data starting from the EAP header, which is not encrypted.
bufferLennumberNoNoData length.

CustomResult

Enumerates the EAP authentication results.

System capability: SystemCapability.Communication.NetManager.Eap

NameValueDescription
RESULT_FAIL0The authentication process ends with a failed result.
RESULT_NEXT1The authentication is successful, and the process proceeds to the next step.
RESULT_FINISH2The authentication process ends with a successful result.

EapMethod

Enumerates the EAP authentication methods.

System capability: SystemCapability.Communication.NetManager.Eap

NameValueDescription
EAP_NONE0Not specified.
EAP_PEAP1PEAP.
EAP_TLS2TLS.
EAP_TTLS3TTLS.
EAP_PWD4Password.
EAP_SIM5SIM.
EAP_AKA6AKA.
EAP_AKA_PRIME7AKA Prime.
EAP_UNAUTH_TLS8UNAUTH TLS.

Phase2Method

Enumerates the Phase 2 authentication methods.

System capability: SystemCapability.Communication.NetManager.Eap

NameValueDescription
PHASE2_NONE0Not specified.
PHASE2_PAP1PAP.
PHASE2_MSCHAP2MS-CHAP.
PHASE2_MSCHAPV23MS-CHAPv2.
PHASE2_GTC4GTC.
PHASE2_SIM5SIM.
PHASE2_AKA6AKA.
PHASE2_AKA_PRIME7AKA Prime.

EthEapProfile

Represents the EAP profile information.

System capability: SystemCapability.Communication.NetManager.Eap

NameTypeRead-OnlyOptionalDescription
eapMethodEapMethodNoNoEAP authentication method.
phase2MethodPhase2MethodNoNoPhase 2 authentication method.
identitystringNoNoIdentity information.
anonymousIdentitystringNoNoAnonymous identity.
passwordstringNoNoPassword.
caCertAliasesstringNoNoCA certificate alias.
caPathstringNoNoCA certificate path.
clientCertAliasesstringNoNoClient certificate alias.
certEntryUint8ArrayNoNoCA certificate content.
certPasswordstringNoNoCA certificate password.
altSubjectMatchstringNoNoA string to match the alternate subject.
domainSuffixMatchstringNoNoA string to match the domain suffix.
realmstringNoNoRealm for the passpoint credential.
plmnstringNoNoPublic land mobile network (PLMN) of the passpoint credential provider.
eapSubIdnumberNoNoSub-ID of the SIM card.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-netconnection-netconn-connectionproperties

openharmony 鸿蒙 errorcode-net-mdns

openharmony 鸿蒙 js-apis-net-ethernet-sys

openharmony 鸿蒙 errorcode-net-http

openharmony 鸿蒙 capi-netstack-http-proxy

openharmony 鸿蒙 js-apis-system-fetch

openharmony 鸿蒙 errorcode-kernel

openharmony 鸿蒙 capi-netstack-netstack-certblob

openharmony 鸿蒙 js-apis-net-mdns

openharmony 鸿蒙 capi-netstack-websocket-closeoption

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/mPjW92wg