Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/cyberdelia/macaroon
- Owner: cyberdelia
- License: apache-2.0
- Created: 2022-02-13T02:14:41.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-26T06:59:11.000Z (2 months ago)
- Last Synced: 2024-08-26T08:57:15.707Z (2 months ago)
- Topics: macaroons
- Language: Kotlin
- Homepage: https://github.com/cyberdelia/macaroon
- Size: 130 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
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);
```