test_class.declarator: CLIENT_ID = 2882303761517520186L
test_class.modifier: private static final
test_class.original_string: private static final long CLIENT_ID = 2882303761517520186L;
test_class.type: long
test_class.var_name: CLIENT_ID
test_class.declarator: REDIRECT_URI = "http://www.zhenchao.com"
test_class.modifier: private static final
test_class.original_string: private static final String REDIRECT_URI = "http://www.zhenchao.com";
test_class.type: String
test_class.var_name: REDIRECT_URI
test_class.declarator: SCOPE = "1 2 4"
test_class.modifier: private static final
test_class.original_string: private static final String SCOPE = "1 2 4";
test_class.type: String
test_class.var_name: SCOPE
test_class.declarator: ALL_SCOPE = "1 2 4 5"
test_class.modifier: private static final
test_class.original_string: private static final String ALL_SCOPE = "1 2 4 5";
test_class.type: String
test_class.var_name: ALL_SCOPE
test_class.declarator: resp4Login
test_class.modifier: private
test_class.original_string: private Response resp4Login;
test_class.type: Response
test_class.var_name: resp4Login
test_class.file: oauth-protocol/src/test/java/org/zhenchao/oauth/controller/AuthCodeGrantControllerTest.java
test_class.identifier: AuthCodeGrantControllerTest
test_class.interfaces: implements RequestPath
test_case.body: @Test
      public void authorizeTest() throws Exception {
          Map<String, Object> params = new HashMap<>();
          params.put("response_type", ResponseType.AUTHORIZATION_CODE.getType());
          params.put("client_id", CLIENT_ID);
          params.put("redirect_uri", REDIRECT_URI);
          Response response = RestAssured.with().params(params).cookies(resp4Login.cookies()).get(PATH_OAUTH_AUTHORIZE_CODE);
          Assert.assertEquals(200, response.getStatusCode());
          // System.out.println(response.asString());
          response = MockUserOperationUtils.authorizationCodeUserAuthorize(response, resp4Login.cookies(), ALL_SCOPE, StringUtils.EMPTY);
          System.out.println(response.asString());
          Assert.assertEquals(302, response.getStatusCode());
          String redirectUrl = response.getHeader("Location");
          System.out.println(redirectUrl);
          response = RestAssured.with().redirects().follow(false).cookies(resp4Login.getCookies()).cookies(response.cookies()).get(redirectUrl);
          System.out.println(response.getHeader("Location"));
          Map<String, String> results = ResultUtils.getLocationUrlParamsValue(response);
          Assert.assertNotNull(results.get("code"));
      }
test_case.class_method_signature: AuthCodeGrantControllerTest.authorizeTest()
test_case.full_signature: @Test public void authorizeTest()
test_case.identifier: authorizeTest
test_case.invocations: put getType put put get cookies params with cookies assertEquals getStatusCode authorizationCodeUserAuthorize cookies println asString assertEquals getStatusCode getHeader println get cookies cookies follow redirects with getCookies cookies println getHeader getLocationUrlParamsValue assertNotNull get
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void authorizeTest()
focal_class.declarator: log = LoggerFactory.getLogger(AuthCodeGrantController.class)
focal_class.modifier: private static final
focal_class.original_string: private static final Logger log = LoggerFactory.getLogger(AuthCodeGrantController.class);
focal_class.type: Logger
focal_class.var_name: log
focal_class.declarator: authorizeRelationService
focal_class.modifier: @Resource
      private
focal_class.original_string: @Resource
      private AuthorizeRelationService authorizeRelationService;
focal_class.type: AuthorizeRelationService
focal_class.var_name: authorizeRelationService
focal_class.declarator: scopeService
focal_class.modifier: @Resource
      private
focal_class.original_string: @Resource
      private ScopeService scopeService;
focal_class.type: ScopeService
focal_class.var_name: scopeService
focal_class.file: oauth-protocol/src/main/java/org/zhenchao/oauth/controller/AuthCodeGrantController.java
focal_class.identifier: AuthCodeGrantController
focal_class.class_method_signature: AuthCodeGrantController.authorize(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                                    @RequestParam("response_type") String responseType,
                                    @RequestParam("client_id") long clientId,
                                    @RequestParam("redirect_uri") String redirectUri,
                                    @RequestParam(name = "scope", required = false) String scope,
                                    @RequestParam(name = "state", required = false) String state,
                                    @RequestParam(name = "skip_confirm", required = false, defaultValue = "false") boolean skipConfirm,
                                    @RequestParam(name = "force_login", required = false, defaultValue = "false") boolean forceLogin)
focal_class.full_signature: @RequestMapping(path = RequestPath.PATH_OAUTH_AUTHORIZE_CODE, method = {GET, POST}) public ModelAndView authorize(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                                    @RequestParam("response_type") String responseType,
                                    @RequestParam("client_id") long clientId,
                                    @RequestParam("redirect_uri") String redirectUri,
                                    @RequestParam(name = "scope", required = false) String scope,
                                    @RequestParam(name = "state", required = false) String state,
                                    @RequestParam(name = "skip_confirm", required = false, defaultValue = "false") boolean skipConfirm,
                                    @RequestParam(name = "force_login", required = false, defaultValue = "false") boolean forceLogin)
focal_class.identifier: authorize
focal_class.modifiers: @RequestMapping(path = RequestPath.PATH_OAUTH_AUTHORIZE_CODE, method = {GET, POST}) public
focal_class.parameters: (HttpServletRequest request, HttpServletResponse response, HttpSession session,
                                    @RequestParam("response_type") String responseType,
                                    @RequestParam("client_id") long clientId,
                                    @RequestParam("redirect_uri") String redirectUri,
                                    @RequestParam(name = "scope", required = false) String scope,
                                    @RequestParam(name = "state", required = false) String state,
                                    @RequestParam(name = "skip_confirm", required = false, defaultValue = "false") boolean skipConfirm,
                                    @RequestParam(name = "force_login", required = false, defaultValue = "false") boolean forceLogin)
focal_class.return: ModelAndView
focal_class.signature: ModelAndView authorize(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                                    @RequestParam("response_type") String responseType,
                                    @RequestParam("client_id") long clientId,
                                    @RequestParam("redirect_uri") String redirectUri,
                                    @RequestParam(name = "scope", required = false) String scope,
                                    @RequestParam(name = "state", required = false) String state,
                                    @RequestParam(name = "skip_confirm", required = false, defaultValue = "false") boolean skipConfirm,
                                    @RequestParam(name = "force_login", required = false, defaultValue = "false") boolean forceLogin)
focal_class.class_method_signature: AuthCodeGrantController.issueToken(HttpServletResponse response,
                                     @RequestParam("grant_type") String grantType,
                                     @RequestParam("code") String code,
                                     @RequestParam("redirect_uri") String redirectUri,
                                     @RequestParam("client_id") long clientId,
                                     @RequestParam(name = "client_secret", required = false) String clientSecret,
                                     @RequestParam(name = "token_type", required = false) String tokenType,
                                     @RequestParam(name = "issue_refresh_token", required = false, defaultValue = "true") boolean refresh)
focal_class.full_signature: @RequestMapping(path = RequestPath.PATH_OAUTH_AUTHORIZE_TOKEN, method = {GET, POST}) public ModelAndView issueToken(HttpServletResponse response,
                                     @RequestParam("grant_type") String grantType,
                                     @RequestParam("code") String code,
                                     @RequestParam("redirect_uri") String redirectUri,
                                     @RequestParam("client_id") long clientId,
                                     @RequestParam(name = "client_secret", required = false) String clientSecret,
                                     @RequestParam(name = "token_type", required = false) String tokenType,
                                     @RequestParam(name = "issue_refresh_token", required = false, defaultValue = "true") boolean refresh)
focal_class.identifier: issueToken
focal_class.modifiers: @RequestMapping(path = RequestPath.PATH_OAUTH_AUTHORIZE_TOKEN, method = {GET, POST}) public
focal_class.parameters: (HttpServletResponse response,
                                     @RequestParam("grant_type") String grantType,
                                     @RequestParam("code") String code,
                                     @RequestParam("redirect_uri") String redirectUri,
                                     @RequestParam("client_id") long clientId,
                                     @RequestParam(name = "client_secret", required = false) String clientSecret,
                                     @RequestParam(name = "token_type", required = false) String tokenType,
                                     @RequestParam(name = "issue_refresh_token", required = false, defaultValue = "true") boolean refresh)
focal_class.return: ModelAndView
focal_class.signature: ModelAndView issueToken(HttpServletResponse response,
                                     @RequestParam("grant_type") String grantType,
                                     @RequestParam("code") String code,
                                     @RequestParam("redirect_uri") String redirectUri,
                                     @RequestParam("client_id") long clientId,
                                     @RequestParam(name = "client_secret", required = false) String clientSecret,
                                     @RequestParam(name = "token_type", required = false) String tokenType,
                                     @RequestParam(name = "issue_refresh_token", required = false, defaultValue = "true") boolean refresh)
focal_method.body: @RequestMapping(path = RequestPath.PATH_OAUTH_AUTHORIZE_CODE, method = {GET, POST})
      public ModelAndView authorize(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                                    @RequestParam("response_type") String responseType,
                                    @RequestParam("client_id") long clientId,
                                    @RequestParam("redirect_uri") String redirectUri,
                                    @RequestParam(name = "scope", required = false) String scope,
                                    @RequestParam(name = "state", required = false) String state,
                                    @RequestParam(name = "skip_confirm", required = false, defaultValue = "false") boolean skipConfirm,
                                    @RequestParam(name = "force_login", required = false, defaultValue = "false") boolean forceLogin)
              throws VerificationException {
  
          log.info("Request authorize code, appId[{}]", clientId);
          ModelAndView mav = new ModelAndView();
  
          // 请求参数封装与校验
          AuthorizeRequestParams requestParams = new AuthorizeRequestParams(responseType, clientId, redirectUri, scope, state);
          ErrorCode validateResult = requestParams.validate();
          if (!ErrorCode.NO_ERROR.equals(validateResult)) {
              // 请求参数有误
              log.error("Request authorize params error, appId[{}], errorCode[{}], params[{}]", clientId, validateResult, requestParams);
              if (ErrorCode.INVALID_CLIENT.equals(validateResult) || ErrorCode.INVALID_REDIRECT_URI.equals(validateResult)) {
                  /*
                   * If the request fails due to a missing, invalid, or mismatching redirection URI,
                   * or if the client identifier is missing or invalid, the authorization server SHOULD inform the resource owner of the
                   * error and MUST NOT automatically redirect the user-agent to the invalid redirection URI.
                   */
                  return JsonView.render(new ResultInfo(validateResult, state), response, false);
              }
              return ResponseUtils.buildErrorResponse(redirectUri, validateResult, state);
          }
  
          AppInfo appInfo = requestParams.getAppInfo();
          UserInfo user = SessionUtils.getUser(session, CookieUtils.get(request, COOKIE_KEY_USER_LOGIN_SIGN));
          if (null == user || forceLogin) {
              // 用户未登录或需要强制登录，跳转到登录页面
              return ResponseUtils.buildLoginResponse(request, appInfo);
          }
          requestParams.setUserInfo(user);
  
          // 获取用户与APP之间的授权关系记录
          Optional<AuthorizeRelation> relation =
                  authorizeRelationService.getAuthorizeRelation(user.getId(), requestParams.getClientId(), requestParams.getScope());
  
          if (relation.isPresent() && skipConfirm) {
              // 用户已授权该APP，下发授权码
              log.info("User had authorized and issue auth code, appId[{}], userId[{}], scope[{}]", clientId, user.getId(), requestParams.getScope());
              AuthorizationCode code = new AuthorizationCode(
                      requestParams.getAppInfo(), user.getId(), relation.get().getScope(), requestParams.getRedirectUri());
              String key = code.getValue();
              if (StringUtils.isBlank(key)) {
                  log.error("Generate auth code error, appId[{}], userId[{}], scope[{}]", clientId, user.getId(), requestParams.getScope());
                  return ResponseUtils.buildErrorResponse(redirectUri, ErrorCode.AUTHORIZATION_CODE_GENERATE_ERROR, state);
              }
              UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(requestParams.getRedirectUri());
              builder.queryParam("code", key);
              if (StringUtils.isNotEmpty(state)) {
                  builder.queryParam("state", state);
              }
              // cache auth code
              AuthCodeCacheHandler.getInstance().put(key, code);
              mav.setViewName("redirect:" + builder.toUriString());
              return mav;
          }
  
          // 用户未授权该APP，跳转到授权页面
          log.info("User not authorized and goto authorize page, appId[{}], userId[{}], relationExist[{}], skipConfirm[{}]",
                  clientId, user.getId(), relation.isPresent(), skipConfirm);
          List<Scope> scopes = scopeService.getScopes(requestParams.getScope());
          return ResponseUtils.buildAuthorizeResponse(request, scopes, user, appInfo, state);
      }
focal_method.class_method_signature: AuthCodeGrantController.authorize(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                                    @RequestParam("response_type") String responseType,
                                    @RequestParam("client_id") long clientId,
                                    @RequestParam("redirect_uri") String redirectUri,
                                    @RequestParam(name = "scope", required = false) String scope,
                                    @RequestParam(name = "state", required = false) String state,
                                    @RequestParam(name = "skip_confirm", required = false, defaultValue = "false") boolean skipConfirm,
                                    @RequestParam(name = "force_login", required = false, defaultValue = "false") boolean forceLogin)
focal_method.full_signature: @RequestMapping(path = RequestPath.PATH_OAUTH_AUTHORIZE_CODE, method = {GET, POST}) public ModelAndView authorize(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                                    @RequestParam("response_type") String responseType,
                                    @RequestParam("client_id") long clientId,
                                    @RequestParam("redirect_uri") String redirectUri,
                                    @RequestParam(name = "scope", required = false) String scope,
                                    @RequestParam(name = "state", required = false) String state,
                                    @RequestParam(name = "skip_confirm", required = false, defaultValue = "false") boolean skipConfirm,
                                    @RequestParam(name = "force_login", required = false, defaultValue = "false") boolean forceLogin)
focal_method.identifier: authorize
focal_method.invocations: info validate equals error equals equals render buildErrorResponse getAppInfo getUser get buildLoginResponse setUserInfo getAuthorizeRelation getId getClientId getScope isPresent info getId getScope getAppInfo getId getScope get getRedirectUri getValue isBlank error getId getScope buildErrorResponse fromHttpUrl getRedirectUri queryParam isNotEmpty queryParam put getInstance setViewName toUriString info getId isPresent getScopes getScope buildAuthorizeResponse
focal_method.modifiers: @RequestMapping(path = RequestPath.PATH_OAUTH_AUTHORIZE_CODE, method = {GET, POST}) public
focal_method.parameters: (HttpServletRequest request, HttpServletResponse response, HttpSession session,
                                    @RequestParam("response_type") String responseType,
                                    @RequestParam("client_id") long clientId,
                                    @RequestParam("redirect_uri") String redirectUri,
                                    @RequestParam(name = "scope", required = false) String scope,
                                    @RequestParam(name = "state", required = false) String state,
                                    @RequestParam(name = "skip_confirm", required = false, defaultValue = "false") boolean skipConfirm,
                                    @RequestParam(name = "force_login", required = false, defaultValue = "false") boolean forceLogin)
focal_method.return: ModelAndView
focal_method.signature: ModelAndView authorize(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                                    @RequestParam("response_type") String responseType,
                                    @RequestParam("client_id") long clientId,
                                    @RequestParam("redirect_uri") String redirectUri,
                                    @RequestParam(name = "scope", required = false) String scope,
                                    @RequestParam(name = "state", required = false) String state,
                                    @RequestParam(name = "skip_confirm", required = false, defaultValue = "false") boolean skipConfirm,
                                    @RequestParam(name = "force_login", required = false, defaultValue = "false") boolean forceLogin)