@@ -10,10 +10,17 @@ import (
1010 "github.com/256dpi/fire/stick"
1111)
1212
13+ // TODO: Allow secret data to be provided with a stored key.
14+ // => Would allow us to use this system for handling invitations.
15+ // => Unique IDs/Labels?
16+
17+ var ErrMissingKey = xo .BF ("missing key" )
18+
1319// Notary is used to issue and verify tokens from keys.
1420type Notary struct {
1521 issuer string
1622 secret []byte
23+ store * coal.Store
1724}
1825
1926// NewNotary creates a new notary with the specified name and secret. It will
@@ -30,15 +37,17 @@ func NewNotary(name string, secret []byte) *Notary {
3037 }
3138
3239 return & Notary {
33- secret : secret ,
3440 issuer : name ,
41+ secret : secret ,
3542 }
3643}
3744
38- // Issue will generate a token from the specified key.
39- func (n * Notary ) Issue (ctx context.Context , key Key ) (string , error ) {
45+ // Issue will generate a token from the specified key. If store is true, the
46+ // key is also saved in the specified store and flagged respectively.
47+ func (n * Notary ) Issue (ctx context.Context , key Key , store bool ) (string , error ) {
4048 // trace
41- _ , span := xo .Trace (ctx , "heat/Notary.Issue" )
49+ ctx , span := xo .Trace (ctx , "heat/Notary.Issue" )
50+ span .Tag ("store" , store )
4251 defer span .End ()
4352
4453 // get meta
@@ -75,6 +84,8 @@ func (n *Notary) Issue(ctx context.Context, key Key) (string, error) {
7584 return "" , err
7685 }
7786
87+ // TODO: Set stored flag in claims?
88+
7889 // issue token
7990 token , err := Issue (n .secret , n .issuer , meta .Name , RawKey {
8091 ID : base .ID .Hex (),
@@ -86,13 +97,38 @@ func (n *Notary) Issue(ctx context.Context, key Key) (string, error) {
8697 return "" , err
8798 }
8899
100+ // return token if not stored
101+ if ! store {
102+ return token , nil
103+ }
104+
105+ // TODO: Set parent.
106+
107+ // otherwise, prepare model
108+ model := Model {
109+ Base : coal .B (base .ID ),
110+ Issued : base .Issued ,
111+ Expires : base .Expires ,
112+ Data : data ,
113+ }
114+
115+ // insert key
116+ err = n .store .M (& model ).Insert (ctx , & model )
117+ if err != nil {
118+ return "" , err
119+ }
120+
89121 return token , nil
90122}
91123
92- // Verify will verify the specified token and fill the specified key.
93- func (n * Notary ) Verify (ctx context.Context , key Key , token string ) error {
124+ // TODO: IssueAndStore()?
125+
126+ // Verify will verify the specified token and fill the specified key. If retrieve
127+ // is true, the key is retrieve from the store and verified to exist and not
128+ // be revoked.
129+ func (n * Notary ) Verify (ctx context.Context , key Key , token string , retrieve bool ) error {
94130 // trace
95- _ , span := xo .Trace (ctx , "heat/Notary.Verify" )
131+ ctx , span := xo .Trace (ctx , "heat/Notary.Verify" )
96132 defer span .End ()
97133
98134 // get meta
@@ -129,5 +165,42 @@ func (n *Notary) Verify(ctx context.Context, key Key, token string) error {
129165 return err
130166 }
131167
168+ // return if not retrieve
169+ if ! retrieve {
170+ return nil
171+ }
172+
173+ // otherwise, retrieve key
174+ var model Model
175+ found , err := n .store .M (& model ).Find (ctx , & model , key .GetBase ().ID , false )
176+ if err != nil {
177+ return err
178+ } else if ! found {
179+ return ErrMissingKey .Wrap ()
180+ }
181+
182+ // TODO: Further validate?
183+
184+ return nil
185+ }
186+
187+ // TODO: VerifyAndRetrieve()?
188+
189+ // Revoke will revoke the specified key by deleting it.
190+ func (n * Notary ) Revoke (ctx context.Context , key Key ) error {
191+ // trace
192+ ctx , span := xo .Trace (ctx , "heat/Notary.Delete" )
193+ defer span .End ()
194+
195+ // TODO: Also revoke descendants?
196+
197+ // delete key
198+ found , err := n .store .M (& Model {}).Delete (ctx , nil , key .GetBase ().ID )
199+ if err != nil {
200+ return err
201+ } else if ! found {
202+ return ErrMissingKey .Wrap ()
203+ }
204+
132205 return nil
133206}
0 commit comments