Issue with JIRA rest api basic authentication

Hi Guys,

I am trying to validate one user’s credential using the JIRA rest api which is hosted on cloud and I am getting 401 (unauthorized). Kindly help me out with this.


	protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {

		SSLTool.certificateValidation();
		String password = authentication.getCredentials().toString();
		HttpHeaders authenticationHeaders = getAuthenticationHeaders(username, password);
		User user = null;
		RestTemplate restTemplate = new RestTemplate();
		HttpEntity<String> request = new HttpEntity<String>(authenticationHeaders);

		List<String> role = new ArrayList<String>();
		try {
			ResponseEntity<String> userDetails = restTemplate.exchange(jiraUrl + JiraRestURLConstants.USER.getURL() + username + "&expand=groups",
					HttpMethod.GET, request, String.class);
			UserJsonParser userParser = new UserJsonParser();

			user = userParser.parse(new JSONObject(userDetails.getBody()));
			Iterator<String> iterator = user.getGroups().getItems().iterator();
			while (iterator.hasNext()) {
				role.add("ROLE_" + iterator.next());
			}

		} catch (Exception e) {
			logger.error(e);
			if (e instanceof JSONException) {
				throw new AuthenticationException(env.getProperty(LDCConstant.JIRA_LOGIN_ERROR)) {
					private static final long serialVersionUID = 1L;
				};
			} else if (e instanceof HttpClientErrorException) {
				HttpClientErrorException ex = (HttpClientErrorException) e;
				if (ex.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
					throw new BadCredentialsException(env.getProperty(LDCConstant.JIRA_UNAUTHORIZED));
				}
			} else if (e instanceof ResourceAccessException) {
				ResourceAccessException ex = (ResourceAccessException) e;
				if (ex.getCause() instanceof UnknownHostException) {
					throw new AuthenticationException(env.getProperty(LDCConstant.JIRA_CONNECTION_ERROR)) {
						private static final long serialVersionUID = 1L;
					};
				} else if (ex.getCause() instanceof ConnectException) {
					throw new AuthenticationException(env.getProperty(LDCConstant.JIRA_SERVER_DOWN)) {
						private static final long serialVersionUID = 1L;
					};
				} else if (ex.getCause() instanceof SocketTimeoutException) {
					throw new AuthenticationException(env.getProperty(LDCConstant.JIRA_CONNECTION_ERROR)) {
						private static final long serialVersionUID = 1L;
					};
				}
			}
		}

		AppUser loadedUser = new AppUser(username, password, AuthorityUtils.createAuthorityList(role.toArray(new String[role.size()])), user.getEmailAddress(),
				user.getDisplayName());
		SecurityContextHolder.getContext().setAuthentication(authentication);
		return loadedUser;
	}

	private HttpHeaders getAuthenticationHeaders(String name, String password) {
		if (name == null && password == null) {
			password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();
			name = SecurityContextHolder.getContext().getAuthentication().getName();
		}
		String base64Creds = getUserCredentialBase64(name, password);
		HttpHeaders headers = new HttpHeaders();
		headers.add("Authorization", "Basic " + base64Creds);
		return headers;
	}

	private String getUserCredentialBase64(String name, String password) {

		String plainCreds = name + ":" + password;
		byte[] plainCredsBytes = plainCreds.getBytes();
		byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
		String base64Creds = new String(base64CredsBytes);
		return base64Creds;
	}

Thanks in advance.

Sounds like: Request Jira REST API wiht Basic Auth doesn't work anymore
and
Jira cloud REST API no longer accepting basic authorisation

1 Like

Content-Type added in headers and it worked. Thanks.

Dear sanjib,Can you please elaborate little bit more like which Content-Type you have added? It will help.Thanks in advance.