You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-login/login/login.go

47 lines
1.0 KiB
Go

7 years ago
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package login
import (
"context"
"time"
)
// Token represents an authorization token.
type Token struct {
Access string
Refresh string
Expires time.Time
}
type key int
const (
tokenKey key = iota
errorKey
)
// WithToken returns a parent context with the token.
func WithToken(parent context.Context, token *Token) context.Context {
return context.WithValue(parent, tokenKey, token)
}
// WithError returns a parent context with the error.
func WithError(parent context.Context, err error) context.Context {
return context.WithValue(parent, errorKey, err)
}
// TokenFrom returns the login token rom the context.
func TokenFrom(ctx context.Context) *Token {
token, _ := ctx.Value(tokenKey).(*Token)
return token
}
// ErrorFrom returns the login error from the context.
func ErrorFrom(ctx context.Context) error {
err, _ := ctx.Value(errorKey).(error)
return err
}