@GET public HttpResult toSignIn(String key, String response_type, String state) { String redirectParam = "?key=" + key + "&response_type=" + response_type + "&state=" + state; if (!response_type.equals("code")) { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=response_type_not_match")); } Client client = Client.DAO.findFirstBy("key=?", key); if (client == null) { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=client_not_found")); } else { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthSignInUrl + redirectParam)); } }
response.setStatus(httpResult.getStatus()); Map<String, String> headers = httpResult.getHeaders(); if (headers != null && headers.size() > 0) { for (Map.Entry<String, String> headersEntry : headers.entrySet()) { List<Cookie> cookies = httpResult.getCookies(); if (cookies != null && cookies.size() > 0) { for (Cookie cookie : cookies) { result = httpResult.getResult(); } else { result = invokeResult;
response.setStatus(httpResult.getStatus()); Map<String, String> headers = httpResult.getHeaders(); if (headers != null && headers.size() > 0) { for (Map.Entry<String, String> headersEntry : headers.entrySet()) { List<Cookie> cookies = httpResult.getCookies(); if (cookies != null && cookies.size() > 0) { for (Cookie cookie : cookies) { result = httpResult.getResult(); } else { result = invokeResult;
@PUT public HttpResult refreshToken(String refresh_token, String grant_type) { String redirectParam = "?refresh_token=" + refresh_token + "&grant_type=" + grant_type; if (!grant_type.equals("refresh_token")) { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=grant_type_not_match")); } Token token = Authorizes.getTokenByRefreshToken(refresh_token); if (token == null) { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=token_not_found")); } else { Client oldClient = Client.DAO.findById(token.getClientId()); //判断账号是否支持该授权方式 if (!Arrays.asList(oldClient.getGrant().split(",")).contains(grant_type)) { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=grant_type_not_support")); } OpenID openID = OpenID.DAO.findFirstBy("user_id=? AND client_id=?", token.getUserId(), token.getClientId()); AccessToken accessToken = new AccessToken(token.getToken(), token.getExpires(), null, openID.<String>get("open_id"), token.getScope()); return new HttpResult(HttpStatus.OK, accessToken); } } }
/** * @param client={"key":"xx","scopeIds":[xx,xx]} * @param response_type * @param state * @return */ @POST public HttpResult getCode(Client client, String response_type, String state) { String redirectParam = "?key=" + client.get("key") + "&response_type=" + response_type + "&state=" + state; if (!response_type.equals("code")) { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=response_type_not_match")); } Client oldClient = Client.DAO.findFirstBy("key=?", client.get("key")); if (oldClient == null) { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=client_not_found")); } else { Set<Scope> scopes = new HashSet<Scope>(); for (Scope scope : oldClient.getScopes()) { if (scope.<Integer>get("selected") == 1 || Arrays.asList(client.getScopeIds()).contains(scope.<Integer>get("id"))) { scopes.add(scope); } } Entity<?> user = Subject.getPrincipal().getModel(); int userId = user.<Integer>get("id"); Code code = new Code(UUID.randomUUID().toString().replaceAll("-", ""), oldClient.<Integer>get("id"), userId, Constant.oauthExpires, scopes); Authorizes.addCode(code);//cache code return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", oldClient.get("redirect_uri") + "?code=" + code.getCode() + "&state=" + state)); } }
String redirectParam = "?key=" + client.get("key") + "&code=" + code + "&grant_type=" + grant_type; if (!grant_type.equals("authorization_code")) { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=grant_type_not_match")); return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=client_or_code_not_found")); } else { return new HttpResult(HttpStatus.FOUND, Maper.<String, String>of("location", Constant.oauthErrorUrl + redirectParam + "&error=grant_type_not_support")); Authorizes.addRefreshToken(refreshToken, token); AccessToken accessToken = new AccessToken(token.getToken(), token.getExpires(), refreshToken, openID.<String>get("open_id"), token.getScope()); return new HttpResult(HttpStatus.OK, accessToken);