add API TOKEN to sessions.

This commit is contained in:
Miroslav Šedivý 2021-03-15 13:34:14 +01:00
parent edfe9adde0
commit 45c9d0c120
2 changed files with 35 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import (
type Session struct { type Session struct {
ImplicitHosting bool ImplicitHosting bool
APIToken string
} }
func (Session) Init(cmd *cobra.Command) error { func (Session) Init(cmd *cobra.Command) error {
@ -15,9 +16,15 @@ func (Session) Init(cmd *cobra.Command) error {
return err return err
} }
cmd.PersistentFlags().String("api_token", "", "API token for interacting with external services")
if err := viper.BindPFlag("api_token", cmd.PersistentFlags().Lookup("api_token")); err != nil {
return err
}
return nil return nil
} }
func (s *Session) Set() { func (s *Session) Set() {
s.ImplicitHosting = viper.GetBool("implicit_hosting") s.ImplicitHosting = viper.GetBool("implicit_hosting")
s.APIToken = viper.GetString("api_token")
} }

View File

@ -14,7 +14,7 @@ import (
) )
func New(config *config.Session) *SessionManagerCtx { func New(config *config.Session) *SessionManagerCtx {
return &SessionManagerCtx{ manager := &SessionManagerCtx{
logger: log.With().Str("module", "session").Logger(), logger: log.With().Str("module", "session").Logger(),
config: config, config: config,
host: nil, host: nil,
@ -24,6 +24,23 @@ func New(config *config.Session) *SessionManagerCtx {
sessionsMu: sync.Mutex{}, sessionsMu: sync.Mutex{},
emmiter: events.New(), emmiter: events.New(),
} }
// create API session
if config.APIToken != "" {
manager.apiSession = &SessionCtx{
id: "API",
token: config.APIToken,
manager: manager,
logger: manager.logger.With().Str("session_id", "API").Logger(),
profile: types.MemberProfile{
Name: "API Session",
IsAdmin: true,
CanLogin: true,
},
}
}
return manager
} }
type SessionManagerCtx struct { type SessionManagerCtx struct {
@ -35,6 +52,7 @@ type SessionManagerCtx struct {
sessions map[string]*SessionCtx sessions map[string]*SessionCtx
sessionsMu sync.Mutex sessionsMu sync.Mutex
emmiter events.EventEmmiter emmiter events.EventEmmiter
apiSession *SessionCtx
} }
func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) (types.Session, string, error) { func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) (types.Session, string, error) {
@ -58,7 +76,7 @@ func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile)
id: id, id: id,
token: token, token: token,
manager: manager, manager: manager,
logger: manager.logger.With().Str("id", id).Logger(), logger: manager.logger.With().Str("session_id", id).Logger(),
profile: profile, profile: profile,
} }
@ -124,11 +142,16 @@ func (manager *SessionManagerCtx) GetByToken(token string) (types.Session, bool)
id, ok := manager.tokens[token] id, ok := manager.tokens[token]
manager.sessionsMu.Unlock() manager.sessionsMu.Unlock()
if !ok { if ok {
return nil, false return manager.Get(id)
} }
return manager.Get(id) // is API session
if manager.apiSession != nil && manager.apiSession.token == token {
return manager.apiSession, true
}
return nil, false
} }
func (manager *SessionManagerCtx) List() []types.Session { func (manager *SessionManagerCtx) List() []types.Session {