Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cyberdelia/macaroon

Cookies with contextual caveats
https://github.com/cyberdelia/macaroon

macaroons

Last synced: 24 days ago
JSON representation

Cookies with contextual caveats

Awesome Lists containing this project

README

        

# Macaroon

## Setup

Add the latest Macaroon version to your project:

### Using Gradle:

```kotlin
implementation("com.lapanthere:macaroon:0.1")
```

### Using Maven:

```xml

com.lapanthere
macaroon
0.1

```

## Getting started

To start, you'll need to generate a secret key:

```kotlin
val key = generateSecretKey()
```

```java
var key = Keys.generateSecretKey()
```

Or using a public/private key setup:

```kotlin
val privateKey = generatePrivateKey()
val publicKey = generatePublicKey()
val key = sharedSecret(publicKey, privateKey)
```

```java
var privateKey = Keys.generatePrivateKey();
var publicKey = Keys.generatePublicKey();
var key = Keys.sharedSecret(publicKey,privateKey);
```

Once you have a secret key, you can now build a macaroon:

```kotlin
val macaroon = buildMacaroon(location = "macaroon/kotlin", identifier = "kotlinUsage", key) {
require("account = 1234")
require(field("actions") containsAll listOf("read", "write"))
require(field("time") lt Instant.now().plusSeconds(60))
}
```

```java
var macaroon = new Macaroon.Builder("macaroon/java", "javaUsage", key)
.require("account = 1234")
.require(field("time").lessThan(Instant.now().plusSeconds(60))
.require(field("actions").containsAll(List.of("read","write")))
.build();
```

You can then verify said macaroon:

```kotlin
val verifier = buildVerifier(macaroon) {
satisfy("account = 1234")
satisfy { caveat -> caveat.isFirstParty }
satisfy("actions", "read", "write")
satisfy("time", Instant.now())
}
verifier.isValid(key)
```

```java
var verifier = new Verifier.Builder(macaroon)
.satisfy("account = 1234")
.satisfy(Caveat::isFirstParty)
.satisfy("admin", true)
.satisfy("actions", List.of("read"), String.class)
.build();
verifier.isValid(key);
```

To de/serialize a macaroon (using the v2 format):

```kotlin
val serialized = macaroon.serialize()
val macaroon = Macaroon(serialized)
```

```java
var serialized = macaroon.serialize();
var macaroon = Macaroon.from(serialized);
```