Search in sources :

Example 1 with OauthAccess

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

the class WeiboOauthComponent method getAccessToken.

/**
 * http://open.weibo.com/wiki/Oauth2/access_token
 */
@Override
public OauthAccess getAccessToken(short siteId, String code) throws ClientProtocolException, IOException {
    OauthConfig config = getConfig(siteId);
    if (CommonUtils.notEmpty(code) && null != config) {
        Map<String, String> paramters = new HashMap<>();
        paramters.put("client_id", config.getAppKey());
        paramters.put("client_secret", config.getAppSecret());
        paramters.put("grant_type", "authorization_code");
        paramters.put("redirect_uri", config.getReturnUrl());
        paramters.put("code", code);
        String html = post("https://api.weibo.com/oauth2/access_token", paramters);
        if (CommonUtils.notEmpty(html)) {
            Map<String, Object> map = objectMapper.readValue(html, new TypeReference<Map<String, Object>>() {
            });
            return new OauthAccess(code, (String) map.get("access_token"), String.valueOf((Integer) map.get("uid")));
        }
    }
    return null;
}
Also used : OauthAccess(com.publiccms.view.pojo.oauth.OauthAccess) HashMap(java.util.HashMap) OauthConfig(com.publiccms.view.pojo.oauth.OauthConfig) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with OauthAccess

use of com.publiccms.view.pojo.oauth.OauthAccess 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 3 with OauthAccess

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

the class QQOauthComponent method getAccessToken.

@Override
public OauthAccess getAccessToken(short siteId, String code) throws ClientProtocolException, IOException {
    OauthConfig config = getConfig(siteId);
    if (CommonUtils.notEmpty(code) && null != config) {
        StringBuilder sb = new StringBuilder("https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&code=");
        sb.append(code).append("&client_id=").append(config.getAppKey()).append("&client_secret=").append(config.getAppSecret()).append("&redirect_uri=").append(config.getReturnUrl());
        String html = get(sb.toString());
        if (CommonUtils.notEmpty(html)) {
            String[] values = html.split("&");
            for (String value : values) {
                if (value.startsWith("access_token=")) {
                    return new OauthAccess(code, value.split("=")[1]);
                }
            }
        }
    }
    return null;
}
Also used : OauthAccess(com.publiccms.view.pojo.oauth.OauthAccess) OauthConfig(com.publiccms.view.pojo.oauth.OauthConfig)

Example 4 with OauthAccess

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

the class WechatOauthComponent method getAccessToken.

/*
     * 
     */
@Override
public OauthAccess getAccessToken(short siteId, String code) throws ClientProtocolException, IOException {
    OauthConfig config = getConfig(siteId);
    if (null != config) {
        StringBuilder sb = new StringBuilder("https://api.weixin.qq.com/sns/oauth2/access_token?appid=");
        sb.append(config.getAppKey()).append("&secret=").append(config.getAppSecret()).append("&code=").append(code).append("&grant_type=authorization_code");
        String html = get(sb.toString());
        Map<String, Object> map = objectMapper.readValue(html, new TypeReference<Map<String, Object>>() {
        });
        if (null != map.get("access_token")) {
            return new OauthAccess(code, (String) map.get("access_token"), (String) map.get("openid"));
        }
    }
    return null;
}
Also used : OauthAccess(com.publiccms.view.pojo.oauth.OauthAccess) OauthConfig(com.publiccms.view.pojo.oauth.OauthConfig) Map(java.util.Map)

Aggregations

OauthAccess (com.publiccms.view.pojo.oauth.OauthAccess)4 OauthConfig (com.publiccms.view.pojo.oauth.OauthConfig)3 Map (java.util.Map)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 OauthUser (com.publiccms.view.pojo.oauth.OauthUser)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Cookie (javax.servlet.http.Cookie)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1