Search in sources :

Example 1 with UserDeviceBean

use of com.akaxin.site.storage.bean.UserDeviceBean in project openzaly by akaxincom.

the class ApiDeviceService method profile.

public CommandResponse profile(Command command) {
    CommandResponse commandResponse = new CommandResponse();
    ErrorCode2 errCode = ErrorCode2.ERROR;
    try {
        ApiDeviceProfileProto.ApiDeviceProfileRequest request = ApiDeviceProfileProto.ApiDeviceProfileRequest.parseFrom(command.getParams());
        String siteUserId = command.getSiteUserId();
        String deviceId = request.getDeviceId();
        LogUtils.requestDebugLog(logger, command, request.toString());
        if (StringUtils.isNotBlank(siteUserId) && StringUtils.isNotBlank(deviceId)) {
            UserDeviceBean deviceBean = UserDeviceDao.getInstance().getDeviceDetails(siteUserId, deviceId);
            DeviceProto.SimpleDeviceProfile deviceProfile = DeviceProto.SimpleDeviceProfile.newBuilder().setDeviceId(String.valueOf(deviceBean.getDeviceId())).setDeviceName(String.valueOf(deviceBean.getDeviceName())).setLastLoginTime(deviceBean.getLoginTime()).build();
            ApiDeviceProfileProto.ApiDeviceProfileResponse response = ApiDeviceProfileProto.ApiDeviceProfileResponse.newBuilder().setDeviceProfile(deviceProfile).setLoginIp(String.valueOf(deviceBean.getDeviceIp())).setLastActiveTime(deviceBean.getActiveTime()).build();
            commandResponse.setParams(response.toByteArray());
            errCode = ErrorCode2.SUCCESS;
        } else {
            errCode = ErrorCode2.ERROR_PARAMETER;
        }
    } catch (Exception e) {
        errCode = ErrorCode2.ERROR_SYSTEMERROR;
        LogUtils.requestErrorLog(logger, command, e);
    }
    return commandResponse.setErrCode2(errCode);
}
Also used : ErrorCode2(com.akaxin.common.constant.ErrorCode2) UserDeviceBean(com.akaxin.site.storage.bean.UserDeviceBean) ApiDeviceProfileProto(com.akaxin.proto.site.ApiDeviceProfileProto) CommandResponse(com.akaxin.common.command.CommandResponse) DeviceProto(com.akaxin.proto.core.DeviceProto)

Example 2 with UserDeviceBean

use of com.akaxin.site.storage.bean.UserDeviceBean in project openzaly by akaxincom.

the class ApiDeviceService method boundList.

/**
 * 获取用户在该站点所有关联设备号
 *
 * @param command
 * @return
 */
public CommandResponse boundList(Command command) {
    CommandResponse commandResponse = new CommandResponse();
    ErrorCode2 errCode = ErrorCode2.ERROR;
    try {
        ApiDeviceBoundListProto.ApiDeviceBoundListRequest request = ApiDeviceBoundListProto.ApiDeviceBoundListRequest.parseFrom(command.getParams());
        String currentUserId = command.getSiteUserId();
        String siteUserId = request.getSiteUserId();
        LogUtils.requestDebugLog(logger, command, request.toString());
        if (StringUtils.isNotBlank(currentUserId) && currentUserId.equals(siteUserId)) {
            ApiDeviceBoundListProto.ApiDeviceBoundListResponse.Builder responseBuilder = ApiDeviceBoundListProto.ApiDeviceBoundListResponse.newBuilder();
            List<UserDeviceBean> deviceList = UserDeviceDao.getInstance().getBoundDevices(siteUserId);
            for (UserDeviceBean device : deviceList) {
                DeviceProto.SimpleDeviceProfile deviceProfile = DeviceProto.SimpleDeviceProfile.newBuilder().setDeviceId(String.valueOf(device.getDeviceId())).setDeviceName(String.valueOf(device.getDeviceName())).setLastLoginTime(device.getActiveTime()).build();
                responseBuilder.addList(deviceProfile);
            }
            commandResponse.setParams(responseBuilder.build().toByteArray());
            errCode = ErrorCode2.SUCCESS;
        } else {
            errCode = ErrorCode2.ERROR_PARAMETER;
        }
    } catch (Exception e) {
        errCode = ErrorCode2.ERROR_SYSTEMERROR;
        LogUtils.requestErrorLog(logger, command, e);
    }
    return commandResponse.setErrCode2(errCode);
}
Also used : ErrorCode2(com.akaxin.common.constant.ErrorCode2) UserDeviceBean(com.akaxin.site.storage.bean.UserDeviceBean) ApiDeviceBoundListProto(com.akaxin.proto.site.ApiDeviceBoundListProto) CommandResponse(com.akaxin.common.command.CommandResponse) DeviceProto(com.akaxin.proto.core.DeviceProto)

Example 3 with UserDeviceBean

use of com.akaxin.site.storage.bean.UserDeviceBean in project openzaly by akaxincom.

the class ApiSiteService method login.

/**
 * 执行用户登陆站点行为
 *
 * @param command
 * @return
 */
public CommandResponse login(Command command) {
    CommandResponse commandResponse = new CommandResponse().setAction(CommandConst.ACTION_RES);
    ErrorCode2 errCode = ErrorCode2.ERROR;
    try {
        ApiSiteLoginProto.ApiSiteLoginRequest loginRequest = ApiSiteLoginProto.ApiSiteLoginRequest.parseFrom(command.getParams());
        String userIdPubk = loginRequest.getUserIdPubk();
        String userIdSignBase64 = loginRequest.getUserIdSignBase64();
        String userDeviceIdPubk = loginRequest.getUserDeviceIdPubk();
        String userDeviceIdSignBase64 = loginRequest.getUserDeviceIdSignBase64();
        String userDeviceName = loginRequest.getUserDeviceName();
        String userToken = loginRequest.getUserToken();
        LogUtils.requestDebugLog(logger, command, loginRequest.toString());
        if (StringUtils.isAnyEmpty(userIdPubk, userIdSignBase64)) {
            errCode = ErrorCode2.ERROR2_LOGGIN_USERID_EMPTY;
            return commandResponse.setErrCode2(errCode);
        }
        if (StringUtils.isAnyEmpty(userDeviceIdPubk, userDeviceIdSignBase64)) {
            errCode = ErrorCode2.ERROR2_LOGGIN_DEVICEID_EMPTY;
            return commandResponse.setErrCode2(errCode);
        }
        // 个人身份公钥,解密Sign签名,解密Key
        PublicKey userPubKey = RSACrypto.getRSAPubKeyFromPem(userIdPubk);
        Signature userSign = Signature.getInstance("SHA512withRSA");
        userSign.initVerify(userPubKey);
        // 原文
        userSign.update(userIdPubk.getBytes());
        boolean userSignResult = userSign.verify(Base64.getDecoder().decode(userIdSignBase64));
        logger.debug("userSignResult={}", userSignResult);
        if (userSignResult) {
            Signature userDeviceSign = Signature.getInstance("SHA512withRSA");
            userDeviceSign.initVerify(userPubKey);
            // 原文
            userDeviceSign.update(userDeviceIdPubk.getBytes());
            userSignResult = userDeviceSign.verify(Base64.getDecoder().decode(userDeviceIdSignBase64));
        }
        logger.debug("deviceSignResult={}", userSignResult);
        // 用户身份校验成功,方可执行登陆操作
        if (userSignResult) {
            // 判断用户,是否已经注册
            SimpleUserBean subean = UserProfileDao.getInstance().getSimpleProfileByPubk(userIdPubk);
            if (subean == null || StringUtils.isEmpty(subean.getUserId())) {
                logger.info("login site: new user need to register before login site");
                // 未注册,告知用户执行注册行为
                errCode = ErrorCode2.ERROR2_LOGGIN_NOREGISTER;
                return commandResponse.setErrCode2(errCode);
            }
            if (subean.getUserStatus() == UserProto.UserStatus.SEALUP_VALUE) {
                logger.info("login site:	 user no permision as seal up");
                // 禁封用户禁止登陆
                errCode = ErrorCode2.ERROR2_LOGGIN_SEALUPUSER;
                return commandResponse.setErrCode2(errCode);
            }
            String siteUserId = subean.getUserId();
            String deviceId = HashCrypto.MD5(userDeviceIdPubk);
            // 保存设备信息
            UserDeviceBean deviceBean = new UserDeviceBean();
            deviceBean.setDeviceId(deviceId);
            deviceBean.setDeviceName(userDeviceName);
            deviceBean.setSiteUserId(siteUserId);
            deviceBean.setUserDevicePubk(userDeviceIdPubk);
            deviceBean.setUserToken(userToken);
            deviceBean.setActiveTime(System.currentTimeMillis());
            deviceBean.setAddTime(System.currentTimeMillis());
            boolean loginResult = SiteLoginDao.getInstance().updateUserDevice(deviceBean);
            if (!loginResult) {
                // 更新失败,则重新保存数据
                loginResult = SiteLoginDao.getInstance().saveUserDevice(deviceBean);
            }
            logger.debug("login site: save device result={} deviceBean={}", loginResult, deviceBean.toString());
            if (loginResult) {
                // 生成session
                String sessionId = UUID.randomUUID().toString();
                UserSessionBean sessionBean = new UserSessionBean();
                sessionBean.setLoginTime(System.currentTimeMillis());
                sessionBean.setSiteUserId(siteUserId);
                sessionBean.setOnline(true);
                sessionBean.setSessionId(sessionId);
                sessionBean.setDeviceId(deviceId);
                // 上次登陆(auth)时间
                sessionBean.setLoginTime(System.currentTimeMillis());
                loginResult = loginResult && SiteLoginDao.getInstance().saveUserSession(sessionBean);
                if (loginResult) {
                    ApiSiteLoginProto.ApiSiteLoginResponse response = ApiSiteLoginProto.ApiSiteLoginResponse.newBuilder().setSiteUserId(siteUserId).setUserSessionId(sessionId).build();
                    commandResponse.setParams(response.toByteArray());
                    errCode = ErrorCode2.SUCCESS;
                } else {
                    errCode = ErrorCode2.ERROR2_LOGGIN_UPDATE_SESSION;
                }
            } else {
                errCode = ErrorCode2.ERROR2_LOGGIN_UPDATE_DEVICE;
            }
        } else {
            errCode = ErrorCode2.ERROR2_LOGGIN_ERRORSIGN;
        }
    } catch (Exception e) {
        errCode = ErrorCode2.ERROR_SYSTEMERROR;
        LogUtils.requestErrorLog(logger, command, e);
    }
    return commandResponse.setErrCode2(errCode);
}
Also used : ApiSiteLoginProto(com.akaxin.proto.site.ApiSiteLoginProto) PublicKey(java.security.PublicKey) UserSessionBean(com.akaxin.site.storage.bean.UserSessionBean) CommandResponse(com.akaxin.common.command.CommandResponse) SimpleUserBean(com.akaxin.site.storage.bean.SimpleUserBean) ErrorCode2(com.akaxin.common.constant.ErrorCode2) UserDeviceBean(com.akaxin.site.storage.bean.UserDeviceBean) Signature(java.security.Signature)

Example 4 with UserDeviceBean

use of com.akaxin.site.storage.bean.UserDeviceBean in project openzaly by akaxincom.

the class SQLiteUserDeviceDao method queryDeviceList.

public List<UserDeviceBean> queryDeviceList(String siteUserId) throws SQLException {
    List<UserDeviceBean> devicesBean = new ArrayList<UserDeviceBean>();
    long startTime = System.currentTimeMillis();
    String sql = "SELECT site_user_id,device_id,user_device_pubk,device_name,active_time FROM " + USER_DEVICE_TABLE + " WHERE site_user_id=?;";
    PreparedStatement preStatement = SQLiteJDBCManager.getConnection().prepareStatement(sql);
    preStatement.setString(1, siteUserId);
    ResultSet rs = preStatement.executeQuery();
    while (rs.next()) {
        UserDeviceBean deviceBean = new UserDeviceBean();
        deviceBean.setSiteUserId(rs.getString(1));
        deviceBean.setDeviceId(rs.getString(2));
        deviceBean.setUserDevicePubk(rs.getString(3));
        deviceBean.setDeviceName(rs.getString(4));
        deviceBean.setActiveTime(rs.getLong(5));
        devicesBean.add(deviceBean);
    }
    LogUtils.dbDebugLog(logger, startTime, devicesBean.size(), sql, siteUserId);
    return devicesBean;
}
Also used : UserDeviceBean(com.akaxin.site.storage.bean.UserDeviceBean) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 5 with UserDeviceBean

use of com.akaxin.site.storage.bean.UserDeviceBean in project openzaly by akaxincom.

the class SQLiteUserDeviceDao method queryLatestDevice.

/**
 * 最新的用户设备,用户最近一次活跃时间算
 *
 * @param siteUserId
 * @return
 * @throws SQLException
 */
public UserDeviceBean queryLatestDevice(String siteUserId) throws SQLException {
    UserDeviceBean deviceBean = new UserDeviceBean();
    long startTime = System.currentTimeMillis();
    String sql = "SELECT site_user_id,device_id,user_device_pubk,device_name,max(active_time) FROM " + USER_DEVICE_TABLE + " WHERE site_user_id=? LIMIT 1;";
    PreparedStatement preStatement = SQLiteJDBCManager.getConnection().prepareStatement(sql);
    preStatement.setString(1, siteUserId);
    ResultSet rs = preStatement.executeQuery();
    if (rs.next()) {
        deviceBean.setSiteUserId(rs.getString(1));
        deviceBean.setDeviceId(rs.getString(2));
        deviceBean.setUserDevicePubk(rs.getString(3));
        deviceBean.setDeviceName(rs.getString(4));
        deviceBean.setActiveTime(rs.getLong(5));
    }
    LogUtils.dbDebugLog(logger, startTime, deviceBean.toString(), sql, siteUserId);
    return deviceBean;
}
Also used : UserDeviceBean(com.akaxin.site.storage.bean.UserDeviceBean) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

UserDeviceBean (com.akaxin.site.storage.bean.UserDeviceBean)9 CommandResponse (com.akaxin.common.command.CommandResponse)5 ErrorCode2 (com.akaxin.common.constant.ErrorCode2)5 DeviceProto (com.akaxin.proto.core.DeviceProto)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)2 ConfigProto (com.akaxin.proto.core.ConfigProto)1 ApiDeviceBoundListProto (com.akaxin.proto.site.ApiDeviceBoundListProto)1 ApiDeviceListProto (com.akaxin.proto.site.ApiDeviceListProto)1 ApiDeviceProfileProto (com.akaxin.proto.site.ApiDeviceProfileProto)1 ApiSecretChatApplyU2Proto (com.akaxin.proto.site.ApiSecretChatApplyU2Proto)1 ApiSiteLoginProto (com.akaxin.proto.site.ApiSiteLoginProto)1 SimpleUserBean (com.akaxin.site.storage.bean.SimpleUserBean)1 UserSessionBean (com.akaxin.site.storage.bean.UserSessionBean)1 PublicKey (java.security.PublicKey)1 Signature (java.security.Signature)1