Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
downgrade tools, add test for signing token
Browse files Browse the repository at this point in the history
  • Loading branch information
friedger committed Jan 21, 2019
1 parent bacd882 commit 09bf98e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
Expand Up @@ -6,6 +6,7 @@ import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import com.eclipsesource.v8.V8ScriptExecutionException
import kotlinx.coroutines.experimental.runBlocking
import org.blockstack.android.sdk.model.Entity
import org.blockstack.android.sdk.model.Profile
import org.blockstack.android.sdk.model.Proof
import org.blockstack.android.sdk.model.toBlockstackConfig
Expand All @@ -21,6 +22,9 @@ import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch


private val PRIVATE_KEY = "a5c61c6ca7b3e7e55edee68566aeab22e4da26baa285c7bd10e8d2218aa3b229"
private val PUBLIC_KEY = "027d28f9951ce46538951e3697c62588a87f1f1f295de4a14fdd4c780fc52cfe69"

private val TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJqdGkiOiJhMzU4MzdmNS1jOGNmLTRiYTMtYjk4ZS03OTY5YTUzZmVjNDgiLCJpYXQiOiIyMDE3LTAzLTA0VDE2OjEzOjA2Ljc5MFoiLCJleHAiOiIyMDE4LTAzLTA0VDE2OjEzOjA2Ljc5MFoiLCJzdWJqZWN0Ijp7InB1YmxpY0tleSI6IjAzZTdhNGQ3OTgzMzY5ZDMzZWQxMzAyMDg4NTk4NWQ2OGY4YjA1ZGVlNjE2OGY3NWY5ZDk3ZTFhMDcyY2RmY2RjNSJ9LCJpc3N1ZXIiOnsicHVibGljS2V5IjoiMDNlN2E0ZDc5ODMzNjlkMzNlZDEzMDIwODg1OTg1ZDY4ZjhiMDVkZWU2MTY4Zjc1ZjlkOTdlMWEwNzJjZGZjZGM1In0sImNsYWltIjp7IkBjb250ZXh0IjoiaHR0cDovL3NjaGVtYS5vcmcvIiwiQHR5cGUiOiJDcmVhdGl2ZVdvcmsiLCJuYW1lIjoiQmFsbG9vbiBEb2ciLCJjcmVhdG9yIjpbeyJAdHlwZSI6IlBlcnNvbiIsIkBpZCI6InRoZXJlYWxqZWZma29vbnMuaWQiLCJuYW1lIjoiSmVmZiBLb29ucyJ9XSwiZGF0ZUNyZWF0ZWQiOiIxOTk0LTA1LTA5VDAwOjAwOjAwLTA0MDAiLCJkYXRlUHVibGlzaGVkIjoiMjAxNS0xMi0xMFQxNDo0NDoyNi0wNTAwIn19.MF7ru91rk8IIKNEEqo9wjLHkvW3jSlcDJmeZZeOVSj9KlXApBp67q_3ke0-LzSO_YyYsUnGOplMYiNxY1XynAA"
private val TOKEN_PUBLIC_KEY = "03e7a4d7983369d33ed13020885985d68f8b05dee6168f75f9d97e1a072cdfcdc5"
private val TOKEN_PUBLIC_KEY_2 = "0354a4b971100da0c0be348fc62a30757d115cb4044adfed3d32e48298f9bcfd0c"
Expand Down Expand Up @@ -110,4 +114,10 @@ class BlockstackSessionTokenTest {

assertThat(result, notNullValue()) // content is dynamic
}

@Test
fun signProfileTokenReturnsSignedToken() {
val tokenPair = session.signProfileToken(Profile(JSONObject(TOKEN_PROFILE_CONTENT)), PRIVATE_KEY, Entity.withKey(TOKEN_PUBLIC_KEY), Entity.withKey(TOKEN_PUBLIC_KEY_2), SigningAlgorithm.ES256K)
assertThat(tokenPair.decodedToken?.signature, notNullValue())
}
}
Expand Up @@ -392,19 +392,19 @@ class BlockstackSession(context: Context? = null, private val config: Blockstack
* @param expiresAt the time of expiration of the token, defaults to next year
* @return the signed profile token
*/
fun signProfileToken(profile: Profile, privateKey: String, subject: Entity, issuer: Entity, signingAlgorithm: SigningAlgorithm = SigningAlgorithm.ESK256, issuedAt: Date = Date(), expiresAt: Date = nextYear()): ProfileToken {
fun signProfileToken(profile: Profile, privateKey: String, subject: Entity, issuer: Entity, signingAlgorithm: SigningAlgorithm = SigningAlgorithm.ES256K, issuedAt: Date = Date(), expiresAt: Date = nextYear()): ProfileTokenPair {
val params = V8Array(v8)
.push(profile.json.toString())
.push(privateKey)
.push(subject.json.toString())
.push(issuer.json.toString())
.push(signingAlgorithm.name)
.push(signingAlgorithm.algorithmName)
.push(issuedAt.toZuluTime())
.push(expiresAt.toZuluTime())

try {
val signedToken = v8blockstackAndroid.executeStringFunction("signProfileToken", params)
return ProfileToken(JSONObject(signedToken))
return wrapProfileToken(signedToken)
} catch (e: Exception) {
Log.d(TAG, e.toString(), e)
throw e
Expand Down
Expand Up @@ -5,9 +5,9 @@ package org.blockstack.android.sdk
*
* @property name identifies the algorithm
*/
enum class SigningAlgorithm(name: String) {
enum class SigningAlgorithm(val algorithmName: String) {
/**
* Represents ECDSA with a P-256K curve.
*/
ESK256 ("ES256K")
ES256K ("ES256K")
}
Expand Up @@ -6,10 +6,26 @@ import org.json.JSONObject
* Object representing a person or organization
*/
class Entity(private val jsonObject: JSONObject) {


/**
* The public key representing this entity
*/
val publicKey: String = jsonObject.getString("publicKey")

/**
* The `JSONObject` that backs this object. You use this object to
* access properties that are not yet exposed by this class.
*/
val json: JSONObject
get() = jsonObject
}

companion object {
fun withKey(publicKey: String): Entity {
val json = JSONObject()
json.put("publicKey", publicKey)
return Entity(json)
}
}
}

Expand Up @@ -7,6 +7,12 @@ import org.json.JSONObject
*/
class ProfileToken(private val jsonObject: JSONObject) {

/**
* The signature if the token was signed
*/
val signature: String? = jsonObject.optString("signature")


/**
* The `JSONObject` that backs this object. You use this object to
* access properties that are not yet exposed by this class.
Expand Down
Expand Up @@ -7,6 +7,20 @@ import org.json.JSONObject
*/
class ProfileTokenPair(private val jsonObject: JSONObject) {

/**
* The encoded token.
*/
val token: String? = jsonObject.optString("token")

/**
* The decoded token.
*/
val decodedToken: ProfileToken? = if (jsonObject.has("decodedToken")) {
ProfileToken(jsonObject.getJSONObject("decodedToken"))
} else {
null
}

/**
* The `JSONObject` that backs this object. You use this object to
* access properties that are not yet exposed by this class.
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
@@ -1,14 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.11'
ext.kotlin_version = '1.2.71'
ext.anko_version = '0.10.4'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit 09bf98e

Please sign in to comment.