File: checkers.go

package info (click to toggle)
golang-github-canonical-candid 1.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,016 kB
  • sloc: python: 1,903; sh: 235; makefile: 81
file content (47 lines) | stat: -rw-r--r-- 1,198 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2017 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package candidtest

import (
	"context"

	"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery/checkers"
	errgo "gopkg.in/errgo.v1"
)

const candidtestNamespace = "github.com/juju/candidclient/candidtest"

var checker = newChecker()

func newChecker() *checkers.Checker {
	ch := checkers.New(nil)
	ch.Namespace().Register(candidtestNamespace, "candidtest")
	ch.Register("discharge-id", candidtestNamespace, checkDischargeID)
	return ch
}

type dischargeIDKey struct{}

func contextWithDischargeID(ctx context.Context, dischargeID string) context.Context {
	return context.WithValue(ctx, dischargeIDKey{}, dischargeID)
}

func dischargeIDFromContext(ctx context.Context) string {
	dischargeID, _ := ctx.Value(dischargeIDKey{}).(string)
	return dischargeID
}

func dischargeIDCaveat(dischargeID string) checkers.Caveat {
	return checkers.Caveat{
		Condition: "discharge-id " + dischargeID,
		Namespace: candidtestNamespace,
	}
}

func checkDischargeID(ctx context.Context, cond, arg string) error {
	if dischargeIDFromContext(ctx) == arg {
		return nil
	}
	return errgo.New("incorrect discharge id")
}