Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Querying Facts returns types that are missing public accessors #69

Open
time4tea opened this issue Aug 6, 2023 · 0 comments
Open

Querying Facts returns types that are missing public accessors #69

time4tea opened this issue Aug 6, 2023 · 0 comments

Comments

@time4tea
Copy link
Contributor

time4tea commented Aug 6, 2023

Thanks for a nice library.

When querying facts on an authorizer, the returned types are com.clevercloud.biscuit.token.builder.Term - this class hierarchy does not have public accessors, so it is difficult to get the data out. (Except for Term.Str, which does have a getValue() )

e.g. https://github.com/biscuit-auth/biscuit-java/blame/master/src/main/java/com/clevercloud/biscuit/token/builder/Term.java#L84

minor: The toStrings look a little bit odd too, e.g. ""+value rather than String.valueOf(x)

sorry my test case is kotlin.

import com.clevercloud.biscuit.crypto.KeyPair
import com.clevercloud.biscuit.token.Biscuit
import com.clevercloud.biscuit.token.builder.Term
import org.junit.jupiter.api.Test
import strikt.api.expectThat
import strikt.assertions.isEqualTo

class BiscuitTest {

    val keypair = KeyPair()

    @Test
    fun `can put a fact in a biscuit and get it back out again`() {

        val token = Biscuit.builder(keypair)
            .add_authority_fact("""email("[email protected]")""")
            .add_authority_fact("""id(123)""")
            .build()

        val tokenString = token.serialize_b64url().replace("=", "")

        val authorizer = Biscuit.from_b64url(tokenString, keypair.public_key())
            .verify(keypair.public_key())
            .authorizer()

        val email = authorizer.query("emailfact(\$name) <- email(\$name)")
            .first { it.name() == "emailfact" }
            .terms()
            .first()
            .stringValue()

        expectThat(email).isEqualTo("[email protected]")
        
        val id = authorizer.query("idfact(\$name) <- id(\$name)")
            .first { it.name() == "idfact" }
            .terms()
            .first()
            .intValue()

        expectThat(id).isEqualTo(123)  // might not work is a Term.Integer an int or a long?
    }
}

fun Term.stringValue(): String {
    return when (this) {
        is Term.Str -> this.value
        else -> throw IllegalArgumentException("Can only call on Term.Str")
    }
}

fun Term.intValue(): Int {
    return when (this) {
        is Term.Integer -> this.value  // does not compile:- package-private, also, is a long?
        else -> throw IllegalArgumentException("Can only call on Term.Integer")
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant