https://github.com/cyberdelia/macaroon
Cookies with contextual caveats
https://github.com/cyberdelia/macaroon
macaroons
Last synced: 5 months ago
JSON representation
Cookies with contextual caveats
- Host: GitHub
- URL: https://github.com/cyberdelia/macaroon
- Owner: cyberdelia
- License: apache-2.0
- Created: 2022-02-13T02:14:41.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-02-03T02:29:06.000Z (over 1 year ago)
- Last Synced: 2025-04-13T14:48:31.476Z (over 1 year ago)
- Topics: macaroons
- Language: Kotlin
- Homepage: https://github.com/cyberdelia/macaroon
- Size: 136 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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);
```