PageRenderTime 51ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/java/com/atlassian/jira/rest/client/auth/BasicHttpAuthenticationHandler.java

https://bitbucket.org/atlassian/jira-rest-java-client
Java | 51 lines | 21 code | 8 blank | 22 comment | 0 complexity | 9904841bf12498e66da72dc593757d0c MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 Atlassian
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.atlassian.jira.rest.client.auth;
  17. import com.atlassian.httpclient.api.Request;
  18. import com.atlassian.jira.rest.client.api.AuthenticationHandler;
  19. import org.apache.commons.codec.binary.Base64;
  20. /**
  21. * Handler for HTTP basic authentication.
  22. * Do NOT use it in with unencrypted HTTP protocol over public networks, as credentials are passed
  23. * effectively in free text.
  24. *
  25. * @since v0.1
  26. */
  27. public class BasicHttpAuthenticationHandler implements AuthenticationHandler {
  28. private static final String AUTHORIZATION_HEADER = "Authorization";
  29. private final String username;
  30. private final String password;
  31. public BasicHttpAuthenticationHandler(final String username, final String password) {
  32. this.username = username;
  33. this.password = password;
  34. }
  35. @Override
  36. public void configure(Request.Builder builder) {
  37. builder.setHeader(AUTHORIZATION_HEADER, "Basic " + encodeCredentials());
  38. }
  39. private String encodeCredentials() {
  40. byte[] credentials = (username + ':' + password).getBytes();
  41. return new String(Base64.encodeBase64(credentials));
  42. }
  43. }