Search in sources :

Example 1 with Oauth

use of com.publiccms.common.api.oauth.Oauth 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 Oauth

use of com.publiccms.common.api.oauth.Oauth in project PublicCMS-preview by sanluan.

the class OauthController method login.

/**
 * @param channel
 * @param returnUrl
 * @param request
 * @param response
 * @return view name
 */
@RequestMapping(value = "login/{channel}")
public String login(@PathVariable("channel") String channel, String returnUrl, HttpServletRequest request, HttpServletResponse response) {
    Oauth oauthComponent = oauthChannelMap.get(channel);
    SysSite site = getSite(request);
    if (null != oauthComponent && oauthComponent.enabled(site.getId())) {
        String state = UUID.randomUUID().toString();
        RequestUtils.addCookie(request.getContextPath(), response, STATE_COOKIE_NAME, state, null, null);
        RequestUtils.addCookie(request.getContextPath(), response, RETURN_URL, returnUrl, null, null);
        return REDIRECT + oauthComponent.getAuthorizeUrl(site.getId(), state);
    }
    return REDIRECT + site.getDynamicPath();
}
Also used : AbstractOauth(com.publiccms.common.base.oauth.AbstractOauth) Oauth(com.publiccms.common.api.oauth.Oauth) SysSite(com.publiccms.entities.sys.SysSite) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

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