summaryrefslogtreecommitdiff
path: root/app/models/auth_session.go
blob: 0b86b1632473c60dd3dd932518a01d69c34cd474 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package models

import (
	"context"
	"strings"
	"time"
)

type AuthSession struct {
	DeviceCode      string `bson:"_id"`
	ClientId        string
	UserCode        string
	AccessCode      string
	Challenge       string
	ChallengeMethod string
	UserId          string
	IsRegistration  bool
	Scope           []string
	Expires         time.Time
	Deleted         *time.Time
}

func NewAuthSession(client string, expires time.Time) *AuthSession {
	return &AuthSession{
		DeviceCode: createDeviceCode(),
		UserCode:   createUserCode(),
		Expires:    expires,
		ClientId:   client,
	}
}

func (s *AuthSession) GenerateAccessCode() {
	s.AccessCode = createDeviceCode()
}

func (s *AuthSession) RecordId() string {
	return s.DeviceCode
}

func (s *AuthSession) MarkDeleted(t time.Time) {
	s.Deleted = &t
}

func (s *AuthSession) SetChallenge(challenge string, method PKCEChallengeType) {
	s.Challenge = challenge
	s.ChallengeMethod = string(method)
}

func (s *AuthSession) SetScopeString(scope string) {
	s.Scope = strings.Split(scope, " ")
}

func (s *AuthSession) HasAnyScopes() bool {
	return len(s.Scope) > 0
}

func (s *AuthSession) HasScope(scope string) bool {
	for _, c := range s.Scope {
		if c == scope {
			return true
		}
	}
	return false
}

type AuthSessionStore interface {
	List(ctx context.Context) ([]*AuthSession, error)
	ListAll(ctx context.Context) ([]*AuthSession, error)
	Get(ctx context.Context, name string) (*AuthSession, error)
	GetByUserCode(ctx context.Context, userCode string) (*AuthSession, error)
	GetByAccessCode(ctx context.Context, userCode string) (*AuthSession, error)
	Insert(ctx context.Context, m *AuthSession) error
	Upsert(ctx context.Context, m *AuthSession) error
	Delete(ctx context.Context, m *AuthSession) error
}