diff --git a/config/cookie.go b/config/cookie.go new file mode 100644 index 0000000..99c617c --- /dev/null +++ b/config/cookie.go @@ -0,0 +1,19 @@ +package config + +type cookieConfig struct { + Domain string + Path string + Secure bool + HttpOnly bool + SameSite string +} + +func DefaultCookieConfig() *cookieConfig { + return &cookieConfig{ + Domain: "", + Path: "", + Secure: false, + HttpOnly: false, + SameSite: "", + } +} diff --git a/config/reader.go b/config/reader.go new file mode 100644 index 0000000..d5a02d5 --- /dev/null +++ b/config/reader.go @@ -0,0 +1,37 @@ +package config + +import "reflect" + +type ConfigInterface interface { + LoadTokenConfig(conf string) (*tokenConfig, error) +} + +var _ ConfigInterface = (*FileConfig)(nil) + +type FileConfig struct { + TokenConfig *tokenConfig +} + +func (c *FileConfig) LoadTokenConfig(conf string) (*tokenConfig, error) { + //TODO implement me + return &tokenConfig{}, nil +} + +func (c *FileConfig) parse(confName string) (err error) { + c.TokenConfig, err = c.LoadTokenConfig(confName) + if err != nil { + return err + } + if reflect.DeepEqual(c.TokenConfig, &tokenConfig{}) { + c.TokenConfig = DefaultTokenConfig() + } + + return err +} + +// NewConfig create from file. +func NewConfig(confName string) (ConfigInterface, error) { + c := &FileConfig{} + err := c.parse(confName) + return c, err +} diff --git a/config/reader_test.go b/config/reader_test.go new file mode 100644 index 0000000..643b9f7 --- /dev/null +++ b/config/reader_test.go @@ -0,0 +1,21 @@ +package config + +import "testing" + +func TestNewConfig(t *testing.T) { + config, err := NewConfig("") + if err != nil { + t.Fatalf("read error: %v", err) + } + t.Log(config) +} + +func TestDefaultCookieConfig(t *testing.T) { + config := DefaultTokenConfig() + t.Log(config) +} + +func TestDefaultTokenConfig(t *testing.T) { + config := DefaultCookieConfig() + t.Log(config) +} diff --git a/config/token.go b/config/token.go new file mode 100644 index 0000000..8327c1e --- /dev/null +++ b/config/token.go @@ -0,0 +1,68 @@ +package config + +type tokenConfig struct { + // TokenName prefix + TokenStyle string + TokenPrefix string + TokenName string + + Timeout int64 + ActivityTimeout int64 + // Data clean period + DataRefreshPeriod int64 + // Auto refresh token + AutoRenew bool + + // Allow multi login + IsConcurrent bool + IsShare bool + // If (IsConcurrent == true && IsShare == false), support MaxLoginCount + MaxLoginCount int16 + + // Read token method + // Set to true to read token from these method before login. + IsReadBody bool + IsReadHeader bool + IsReadCookie bool + + // Write token to response header. + // Set to true to write after login. + IsWriteHeader bool + + TokenSessionCheckLogin bool + + JwtSecretKey string + + CurDomain string + + SameTokenTimeout int64 + + CheckSameToken bool + + CookieConfig *cookieConfig +} + +func DefaultTokenConfig() *tokenConfig { + return &tokenConfig{ + TokenStyle: "uuid", + TokenPrefix: "", + TokenName: "tokenGo", + Timeout: 60 * 60 * 24 * 30, + ActivityTimeout: -1, + DataRefreshPeriod: 30, + AutoRenew: true, + IsConcurrent: true, + IsShare: true, + MaxLoginCount: 12, + IsReadBody: true, + IsReadHeader: true, + IsReadCookie: true, + IsWriteHeader: false, + TokenSessionCheckLogin: true, + JwtSecretKey: "", + CurDomain: "", + SameTokenTimeout: 60 * 60 * 24, + CheckSameToken: false, + CookieConfig: DefaultCookieConfig(), + } +}