Skip to content
This repository
Browse code

support for oauth authorization

  • Loading branch information...
commit a944e7bf305319b019452aeb0450387a0be526bf 1 parent 16363cf
doug tangren authored
24  app/src/main/scala/authorize.scala
... ...
@@ -0,0 +1,24 @@
  1
+package giter8
  2
+
  3
+import dispatch._
  4
+import dispatch.liftjson.Js._
  5
+import net.liftweb.json.JsonAST._
  6
+
  7
+object Authorize {
  8
+  val authorizations = :/("api.github.com").secure / "authorizations"
  9
+}
  10
+
  11
+trait Authorize { self: Giter8 =>
  12
+  import Authorize._
  13
+  
  14
+  def auth(user: String, pass: String): Either[String, String] =
  15
+    http(authorizations.POST.as_!(user, pass) ># { _ \ "token" match {
  16
+      case JString(tok) => Some(tok)
  17
+      case _ => None
  18
+    }}).map { access =>
  19
+      Config.properties { 
  20
+        _.setProperty("gh.access", access)
  21
+      }
  22
+      Right("Authorization stored")
  23
+    } getOrElse Left("Authorization failed")
  24
+}
26  app/src/main/scala/config.scala
... ...
@@ -0,0 +1,26 @@
  1
+package giter8
  2
+
  3
+object Config {
  4
+  import java.io.{ File, FileInputStream, FileOutputStream }
  5
+  import java.util.Properties
  6
+  
  7
+  val file = new File(Giter8.home, "config")
  8
+
  9
+  def get(name: String) =
  10
+    Option(properties {
  11
+      _.getProperty(name)
  12
+    })
  13
+  
  14
+  def properties[A](f: Properties => A): A = {
  15
+    if (!file.exists()) file.getParentFile().mkdirs()
  16
+    val p = new Properties()
  17
+    GIO.use(new FileInputStream(file)) { in =>
  18
+      p.load(in)
  19
+    }
  20
+    val result = f(p)
  21
+    GIO.use(new FileOutputStream(file)) { out =>
  22
+      p.store(out, null)
  23
+    }
  24
+    result
  25
+  }
  26
+}
7  app/src/main/scala/credentials.scala
@@ -2,10 +2,15 @@ package giter8
2 2
 
3 3
 trait Credentials { self: Apply =>
4 4
   import scala.util.control.Exception.allCatch
  5
+  import dispatch._
5 6
   
6 7
   def withCredentials(req: dispatch.Request) =
7  
-    credentials map { case (user, pass) => req as_! (user, pass) } getOrElse req
  8
+    (oauth map { case token => req <:< Map("Authorization" -> "token %s".format(token)) }).orElse(
  9
+      credentials map { case (user, pass) => req as_! (user, pass) }) getOrElse req
8 10
   
  11
+  def oauth: Option[String] =
  12
+    Config.get("gh.oauth")
  13
+
9 14
   lazy val credentials: Option[(String, String)] =
10 15
     gitConfig("github.user") flatMap { user =>
11 16
       gitConfig("github.password") map { password =>
11  app/src/main/scala/giter8.scala
... ...
@@ -1,11 +1,13 @@
1 1
 package giter8
2 2
 
3  
-class Giter8 extends xsbti.AppMain with Discover with Apply with Credentials {
  3
+class Giter8 extends xsbti.AppMain
  4
+  with Discover with Apply with Authorize with Credentials {
4 5
   import dispatch._
5 6
 
6 7
   val Repo = """^(\S+)/(\S+?)(?:\.g8)?$""".r
7 8
   val Branch = """^-(b|-branch)$""".r
8 9
   val RemoteTemplates = """^-(l|-list)$""".r
  10
+  val Auth = """^-(a|-auth)$""".r
9 11
 
10 12
   java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.SEVERE)
11 13
 
@@ -20,6 +22,8 @@ class Giter8 extends xsbti.AppMain with Discover with Apply with Credentials {
20 22
         inspect("%s/%s.g8".format(user, proj), None, params)
21 23
       case (params, Array(Repo(user, proj), Branch(_), branch)) =>
22 24
         inspect("%s/%s.g8".format(user, proj), Some(branch), params)
  25
+      case (params, Array(Auth(_), user, pass)) =>
  26
+        auth(user, pass)
23 27
       case _ => Left(usage)
24 28
     }) fold ({ error =>
25 29
       System.err.println("\n%s\n" format error)
@@ -68,6 +72,11 @@ class Giter8 extends xsbti.AppMain with Discover with Apply with Credentials {
68 72
 class Exit(val code: Int) extends xsbti.Exit
69 73
 
70 74
 object Giter8 extends Giter8 {
  75
+  import java.io.File
  76
+  val home = Option(System.getProperty("G8_HOME")).map(new File(_)).getOrElse(
  77
+    new File(System.getProperty("user.home"), ".g8")
  78
+  )
  79
+
71 80
   /** Main-class runner just for testing from sbt*/
72 81
   def main(args: Array[String]) {
73 82
     System.exit(run(args))

0 notes on commit a944e7b

Please sign in to comment.
Something went wrong with that request. Please try again.