[#283] pkg/session: Implement work with token contexts

Implement `Context` / `SetContext` methods on `Token` which reads / sets
token context. Support container context (`ContainerContext`).

Add helper function `GetContainerContext` for easy reading of the container
context.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-28 13:16:05 +03:00 committed by Leonard Lyubich
parent 2648abb49e
commit 6cd3497388
2 changed files with 96 additions and 0 deletions

View file

@ -135,6 +135,50 @@ func (t *Token) Signature() *pkg.Signature {
)
}
// SetContext sets context of the Token.
//
// Supported contexts:
// - *ContainerContext.
//
// Resets context if it is not supported.
func (t *Token) SetContext(v interface{}) {
var cV2 session.SessionTokenContext
switch c := v.(type) {
case *ContainerContext:
cV2 = c.ToV2()
}
t.setBodyField(func(body *session.SessionTokenBody) {
body.SetContext(cV2)
})
}
// Context returns context of the Token.
//
// Supports same contexts as SetContext.
//
// Returns nil if context is not supported.
func (t *Token) Context() interface{} {
switch v := (*session.SessionToken)(t).
GetBody().
GetContext(); c := v.(type) {
default:
return nil
case *session.ContainerSessionContext:
return ContainerContextFromV2(c)
}
}
// GetContainerContext is a helper function that casts
// Token context to ContainerContext.
//
// Returns nil if context is not a ContainerContext.
func GetContainerContext(t *Token) *ContainerContext {
c, _ := t.Context().(*ContainerContext)
return c
}
// Marshal marshals Token into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.