markusthoemmes commented on a change in pull request #3788: Store and pass 
variant data in AuthKey
URL: 
https://github.com/apache/incubator-openwhisk/pull/3788#discussion_r196834704
 
 

 ##########
 File path: common/scala/src/main/scala/whisk/core/entity/AuthKey.scala
 ##########
 @@ -17,24 +17,31 @@
 
 package whisk.core.entity
 
+import akka.http.scaladsl.model.headers.{BasicHttpCredentials, HttpCredentials}
 import spray.json._
 import spray.json.DefaultJsonProtocol._
 
+trait AuthKeyEnv {
+  val authElements: Map[String, String]
+  def toEnvironment = authElements.toJson.asJsObject
+  def getCredentials: HttpCredentials
+}
+
 /**
  * Authentication key, consisting of a UUID and Secret.
  *
- * It is a value type (hence == is .equals, immutable and cannot be assigned 
null).
  * The constructor is private so that argument requirements are checked and 
normalized
  * before creating a new instance.
  *
  * @param k (uuid, key) the uuid and key, assured to be non-null because both 
types are values
  */
-protected[core] class AuthKey private (private val k: (UUID, Secret)) extends 
AnyVal {
-  def uuid: UUID = k._1
-  def key: Secret = k._2
-  def revoke = new AuthKey(uuid, Secret())
-  def compact: String = s"$uuid:$key"
+protected[core] case class AuthKey private (authElements: Map[String, String]) 
extends AuthKeyEnv {
+  def uuid: UUID = UUID(this.authElements("api_key").split(":")(0))
+  def key: Secret = Secret(this.authElements("api_key").split(":")(1))
+  def revoke = AuthKey(uuid, Secret())
+  def compact: String = this.authElements("api_key")
   override def toString: String = uuid.toString
+  override def getCredentials: HttpCredentials = 
BasicHttpCredentials(uuid.asString, key.asString)
 }
 
 Review comment:
   I feel like this class should stay mostly the same as today. In this 
implementation, each call to `uuid` would (unsafely) get an element from the 
map, split it, and (unsafely) get it's 0 index.
   
   For an AuthKey, we can statically know, that it contains both UUID and 
Secret at compile time.
   
   ```scala
   protected[core] case class AuthKey private (private val k: (UUID, Secret)) 
extends AuthenticationCredentials {
     def uuid: UUID = k._1
     def key: Secret = k._2
     def revoke = new AuthKey(uuid, Secret())
     def compact: String = s"$uuid:$key"
   
     override def toEnvironment = JsObject("api_key" -> 
s"${uuid.asString}:${key.asString}".toJson)
     override def getCredentials: HttpCredentials = 
BasicHttpCredentials(uuid.asString, key.asString)
   }
   ```
   
   Would be the implementation then.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to