PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/opup/src/main/scala/com/atlassian/labs/opup/HttpAuthentication.scala

https://bitbucket.org/jwalton/opup
Scala | 64 lines | 50 code | 13 blank | 1 comment | 2 complexity | 98eaa4932510514255ea0a15430cf32d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.labs.opup
  2. import java.io.BufferedReader
  3. import java.io.IOException
  4. import java.io.InputStreamReader
  5. import java.net.Authenticator
  6. import java.net.PasswordAuthentication
  7. import org.apache.http.auth.AuthScope
  8. import org.apache.http.auth.Credentials
  9. import org.apache.http.auth.UsernamePasswordCredentials
  10. import org.apache.http.client.CredentialsProvider
  11. object HttpAuthentication {
  12. def hardcoded(auth: PasswordAuthentication): Authenticator = {
  13. return new Authenticator {
  14. override def getPasswordAuthentication(): PasswordAuthentication = auth
  15. }
  16. }
  17. def fromConsole = ConsolePasswordAuthenticator
  18. def anonymous = new Authenticator{}
  19. }
  20. object ConsolePasswordAuthenticator extends CredentialsProvider {
  21. def clear() {}
  22. def setCredentials(x: AuthScope, y: Credentials) {}
  23. def getCredentials(x: AuthScope): Credentials = {
  24. val con = System.console();
  25. if (con != null)
  26. {
  27. con.printf("Maven repository requires authentication.\n");
  28. val username = con.readLine("Username: ");
  29. val password = new String(con.readPassword("Password: "))
  30. new UsernamePasswordCredentials(username, password)
  31. }
  32. else
  33. {
  34. try
  35. {
  36. /* Hello, Eclipse! */
  37. System.out.print("Maven repository requires authentication.\n");
  38. val br = new BufferedReader(new InputStreamReader(System.in));
  39. System.out.println("Username: ");
  40. val username = br.readLine();
  41. System.out.println("Password: ");
  42. val password = br.readLine()
  43. new UsernamePasswordCredentials(username, password)
  44. }
  45. catch
  46. {
  47. case ioe: IOException => throw new RuntimeException("Unable to get username and password", ioe)
  48. }
  49. }
  50. }
  51. }