-
Notifications
You must be signed in to change notification settings - Fork 125
/
auth.go
52 lines (38 loc) · 1.16 KB
/
auth.go
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
// Copyright 2024 PingCAP, Inc. Licensed under Apache-2.0.
package utils
import (
"time"
"github.com/gin-gonic/gin"
)
type AuthType int
const SessionVersion = 2
// The content of this structure will be encrypted and stored as both Session Token and Sharing Token.
// For fields that don't need to be cloned during session sharing, mark fields as `msgpack:"-"`.
type SessionUser struct {
// Must be 2. This field is used to invalidate outdated sessions after schema change.
Version int
DisplayName string
HasTiDBAuth bool
TiDBUsername string
TiDBPassword string
// This field only exists for CodeAuth.
SharedSessionExpireAt time.Time `msgpack:"-" json:",omitempty"`
// This field only exists for SSOAuth
OIDCIDToken string `json:",omitempty"`
// These fields should not be updated by individual authenticators.
AuthFrom AuthType `msgpack:"-" json:",omitempty"`
// TODO: Make them table fields
IsShareable bool
IsWriteable bool
}
const (
// The key that attached the SessionUser in the gin Context.
SessionUserKey = "user"
)
func GetSession(c *gin.Context) *SessionUser {
i, ok := c.Get(SessionUserKey)
if !ok {
return nil
}
return i.(*SessionUser)
}