package com.casic.missiles.nbiot;
import com.casic.missiles.util.JsonUtil;
import com.casic.missiles.util.StreamClosedHttpResponse;
import com.casic.missiles.model.response.SuccessResponseData;
import com.casic.missiles.util.HttpsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* Auth:
* This interface is used to authenticate third-party systems before third-party systems access open APIs.
*/
public class Authentication {
public static Logger log = LoggerFactory.getLogger(Authentication.class);
@SuppressWarnings({ "resource", "unchecked" })
public static SuccessResponseData doAuth(String baseUrl, String appId, String secret) throws Exception {
String urlLogin = baseUrl+Constant.APP_AUTH;
// Two-Way Authentication
HttpsUtil httpsUtil = new HttpsUtil();
try {
httpsUtil.initSSLConfigForTwoWay();
} catch (Exception e) {
e.printStackTrace();
return new SuccessResponseData(500,null,"https证书文件不存在,连接失败");
}
Map<String, String> param = new HashMap<>();
param.put("appId", appId);
param.put("secret", secret);
StreamClosedHttpResponse responseLogin = httpsUtil.doPostFormUrlEncodedGetStatusLine(urlLogin, param);
log.info("NBCUCC_login_response:"+responseLogin.getStatusLine());
log.info(responseLogin.getContent());
//获取状态码
int code = responseLogin.getStatusLine().getStatusCode();
if(code==200){
//resolve the value of accessToken from responseLogin.
Map<String, String> data = new HashMap<>();
data = JsonUtil.jsonString2SimpleObj(responseLogin.getContent(), data.getClass());
String accessToken = data.get("accessToken");
return new SuccessResponseData(200,accessToken,"注册设备成功");
}else{
return new SuccessResponseData(code,null,"连接电信平台失败,错误码为"+code);
}
}
}