Search in sources :

Example 1 with OauthUser

use of com.publiccms.view.pojo.oauth.OauthUser in project PublicCMS-preview by sanluan.

the class OauthController method callback.

/**
 * @param channel
 * @param state
 * @param code
 * @param request
 * @param session
 * @param response
 * @param model
 * @return view name
 */
@RequestMapping(value = "callback/{channel}")
public String callback(@PathVariable("channel") String channel, String state, String code, HttpServletRequest request, HttpSession session, HttpServletResponse response, ModelMap model) {
    Oauth oauthComponent = oauthChannelMap.get(channel);
    SysSite site = getSite(request);
    Cookie stateCookie = RequestUtils.getCookie(request.getCookies(), STATE_COOKIE_NAME);
    if (null != oauthComponent && oauthComponent.enabled(site.getId()) && null != stateCookie && null != state && state.equals(stateCookie.getValue())) {
        try {
            OauthAccess oauthAccess = oauthComponent.getOpenId(site.getId(), code);
            if (null != oauthAccess && null != oauthAccess.getOpenId()) {
                Cookie cookie = RequestUtils.getCookie(request.getCookies(), RETURN_URL);
                String returnUrl = site.getDynamicPath();
                if (null != cookie && null != cookie.getValue()) {
                    returnUrl = cookie.getValue();
                }
                SysUserToken entity = sysUserTokenService.getEntity(oauthAccess.getOpenId());
                if (null != entity) {
                    if (entity.getChannel().equals(channel)) {
                        setUserToSession(session, sysUserService.getEntity(entity.getUserId()));
                        return REDIRECT + returnUrl;
                    }
                } else {
                    SysUser user = getUserFromSession(session);
                    if (null == user) {
                        OauthUser oauthUser = oauthComponent.getUserInfo(site.getId(), oauthAccess);
                        Map<String, String> config = configComponent.getConfigData(site.getId(), AbstractOauth.CONFIG_CODE);
                        if (null != oauthUser && CommonUtils.notEmpty(config) && CommonUtils.notEmpty(config.get(LoginConfigComponent.CONFIG_REGISTER_URL))) {
                            model.addAttribute("nickname", oauthUser.getNickname());
                            model.addAttribute("openId", oauthUser.getOpenId());
                            model.addAttribute("avatar", oauthUser.getAvatar());
                            model.addAttribute("gender", oauthUser.getGender());
                            model.addAttribute("channel", channel);
                            model.addAttribute("returnUrl", returnUrl);
                            return REDIRECT + config.get(LoginConfigComponent.CONFIG_REGISTER_URL);
                        }
                    } else {
                        String authToken = new StringBuilder(channel).append(DOT).append(site.getId()).append(DOT).append(oauthAccess.getOpenId()).toString();
                        entity = new SysUserToken(authToken, site.getId(), user.getId(), channel, CommonUtils.getDate(), RequestUtils.getIpAddress(request));
                        sysUserTokenService.save(entity);
                        setUserToSession(session, user);
                        return REDIRECT + returnUrl;
                    }
                }
            }
        } catch (IOException e) {
            log.error(e);
        }
    }
    return REDIRECT + site.getDynamicPath();
}
Also used : AbstractOauth(com.publiccms.common.base.oauth.AbstractOauth) Oauth(com.publiccms.common.api.oauth.Oauth) Cookie(javax.servlet.http.Cookie) OauthAccess(com.publiccms.view.pojo.oauth.OauthAccess) SysUserToken(com.publiccms.entities.sys.SysUserToken) SysUser(com.publiccms.entities.sys.SysUser) IOException(java.io.IOException) SysSite(com.publiccms.entities.sys.SysSite) OauthUser(com.publiccms.view.pojo.oauth.OauthUser) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with OauthUser

use of com.publiccms.view.pojo.oauth.OauthUser in project PublicCMS-preview by sanluan.

the class QQOauthComponent method getUserInfo.

/*
     * http://wiki.connect.qq.com/get_user_info
     */
@Override
public OauthUser getUserInfo(short siteId, OauthAccess oauthAccess) throws ClientProtocolException, IOException {
    OauthConfig config = getConfig(siteId);
    if (null != oauthAccess && null != config) {
        StringBuilder sb = new StringBuilder("https://graph.qq.com/user/get_user_info?access_token=");
        sb.append(oauthAccess.getAccessToken()).append("&oauth_consumer_key=").append(config.getAppKey()).append("&openid=").append(oauthAccess.getOpenId()).append("&format=format");
        String html = get(sb.toString());
        if (CommonUtils.notEmpty(html)) {
            Map<String, Object> map = objectMapper.readValue(html, new TypeReference<Map<String, Object>>() {
            });
            if (0 == (Integer) map.get("ret")) {
                return new OauthUser(oauthAccess.getOpenId(), (String) map.get("nickname"), (String) map.get("figureurl_qq_2"), "男".equals(map.get("gender")) ? "m" : "f");
            }
        }
    }
    return null;
}
Also used : OauthConfig(com.publiccms.view.pojo.oauth.OauthConfig) Map(java.util.Map) OauthUser(com.publiccms.view.pojo.oauth.OauthUser)

Aggregations

OauthUser (com.publiccms.view.pojo.oauth.OauthUser)2 Oauth (com.publiccms.common.api.oauth.Oauth)1 AbstractOauth (com.publiccms.common.base.oauth.AbstractOauth)1 SysSite (com.publiccms.entities.sys.SysSite)1 SysUser (com.publiccms.entities.sys.SysUser)1 SysUserToken (com.publiccms.entities.sys.SysUserToken)1 OauthAccess (com.publiccms.view.pojo.oauth.OauthAccess)1 OauthConfig (com.publiccms.view.pojo.oauth.OauthConfig)1 IOException (java.io.IOException)1 Map (java.util.Map)1 Cookie (javax.servlet.http.Cookie)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1