server

package
v0.0.0-...-38ffd7d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2019 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecryptAesEcbBase64

func DecryptAesEcbBase64(data, key string) ([]byte, error)

DecryptAesEcbBase64 DecryptAesEcbBase64 decode data with base64 and decrypt with key using AES-ECB.

The key should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256

func EncryptAesEcbBase64

func EncryptAesEcbBase64(data []byte, key string) (string, error)

EncryptAesEcbBase64 EncryptAesEcbBase64 encrypt data with key using AES-ECB and encode with base64.

The key should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256

func MD5

func MD5(src string) string

func RandMd5

func RandMd5() string

func RandToken

func RandToken() string

RandToken RandToken general a 32 bytes random string.

Types

type App

type App struct {
	AppID           string `json:"app_id"`
	Secret          string `json:"secret"`
	Name            string `json:"name"`
	Desc            string `json:"description"`
	OIDCRedirectURI string `json:"oidc_redirect_uri"`
	UpdateAt        string `json:"update_at"`
}

type AppCreateParam

type AppCreateParam struct {
	Name            string `json:"name"`
	Desc            string `json:"description"`
	OIDCRedirectURI string `json:"oidc_redirect_uri"`
}

type AppDeleteParam

type AppDeleteParam struct {
	AppID string `json:"app_id"`
}

type AppUpdateParam

type AppUpdateParam struct {
	AppID           string `json:"app_id"`
	Name            string `json:"name"`
	Desc            string `json:"description"`
	OIDCRedirectURI string `json:"oidc_redirect_uri"`
}

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

Auth a http auth service

Http Auth Type - With Cookie (session key) - With Bearer Token (session key) - with Bearer Token (jwt token)

func NewAuth

func NewAuth(db DB, log Logger, mail *Mail) *Auth

NewAuth create Auth entity

func (*Auth) CheckCaptcha

func (auth *Auth) CheckCaptcha(username, captcha string) error

CheckCaptcha check captcha is valid

func (*Auth) CheckUserExist

func (auth *Auth) CheckUserExist(username string) bool

CheckUserExist CheckUserExist check the username has been register.

func (*Auth) GetUser

func (auth *Auth) GetUser(r *http.Request) (*AuthUser, error)

GetUser get user info from auth info

Here we use cookie (session key) first

func (*Auth) Login

func (auth *Auth) Login(
	w http.ResponseWriter,
	r *http.Request,
	username string,
	remember bool,
) string

Login just login with username

No need to check password here

func (*Auth) Logout

func (auth *Auth) Logout(w http.ResponseWriter, r *http.Request)

Logout logout all ticket

func (*Auth) Register

func (auth *Auth) Register(username, password string) error

Register register user

func (*Auth) ResetPassword

func (auth *Auth) ResetPassword(username, password string) error

ResetPassword reset user password

func (*Auth) SendCaptcha

func (auth *Auth) SendCaptcha(username, subject, template string) error

SendCaptcha cache captcha and send

type AuthUser

type AuthUser struct {
	ID        int64  `json:"id"`
	Username  string `json:"username"`
	LastLogin string `json:"last_login"`
}

AuthUser authed user entity

type Authorization

type Authorization struct {
	ID       int64  `json:"id"`
	Name     string `json:"name"`
	Desc     string `json:"description"`
	CreateAt string `json:"create_at"`
}

type AuthorizationDeleteParam

type AuthorizationDeleteParam struct {
	ID string `json:"id"`
}

type Config

type Config struct{}

Config load and hold config

func NewConfig

func NewConfig() *Config

NewConfig create Config entity

func (*Config) Get

func (c *Config) Get(key string) string

Get get env value by key

func (*Config) Int

func (c *Config) Int(key string) int

Int get int env value

func (*Config) Set

func (c *Config) Set(key, value string)

Set set env kv

type DB

type DB interface {
	BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

DB The sql database interface

type Kerberos

type Kerberos struct {
	// contains filtered or unexported fields
}

Kerberos Kerberos is a service implement kerberos protocal.

As an example, this project has using kerberos for user login.

func NewKerberos

func NewKerberos(db DB, log Logger, tgsSecretKey, appSecretKey string) *Kerberos

NewKerberos NewKerberos create a Kerberos service with settings.

func (*Kerberos) Auth

func (k *Kerberos) Auth(username string) (*KerberosAuthResult, error)

Auth Auth implement the process of kerberos auth.

func (*Kerberos) Grant

func (k *Kerberos) Grant(encTGT, appID, encAuthenticator string) (*KerberosGrantResult, error)

Grant Grant implement the process of kerberos grant.

func (*Kerberos) Login

func (k *Kerberos) Login(encST, encAuthenticator string) (string, error)

Login Login usually using in application service, for validating the kerberos service ticket.

type KerberosAuthResult

type KerberosAuthResult struct {
	EncCTSK string `json:"ctsk"`
	EncTGT  string `json:"tgt"`
}

KerberosAuthResult KerberosAuthResult is the result of kerberos auth.

type KerberosGrantResult

type KerberosGrantResult struct {
	EncCSSK string `json:"cssk"`
	EncST   string `json:"st"`
}

KerberosGrantResult KerberosGrantResult is the result of kerberos grant.

type Logger

type Logger interface {
	Fatal(v ...interface{})
	Fatalf(format string, v ...interface{})
	Fatalln(v ...interface{})
	Print(v ...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

Logger The Log interface

type Mail

type Mail struct {
	// contains filtered or unexported fields
}

Mail Mail is a mail micro service The mail micro service could be using as a admin mailer, which could send mail to users.

func NewMail

func NewMail(
	log Logger,
	username,
	password,
	host,
	address,
	from,
	templatePath string,
) *Mail

NewMail NewMail returns a mail micro service with account config.

The username and password is used to authenticate to host.

The address must include a port, as in "mail.example.com:smtp".

The tempaltePath provide template for sending a html mail.

func (*Mail) Send

func (m *Mail) Send(to []string, subject, templateName string, data interface{}) error

Send Send a mail to "to", whose body is the template render with data.

type Oidc

type Oidc struct {
	Keys []*jwt.RsaKeySet `json:"keys"`
	// contains filtered or unexported fields
}

Oidc oidc service

func NewOidc

func NewOidc(db DB, log Logger) *Oidc

NewOidc create oidc entity

func (*Oidc) AddKeyPair

func (oidc *Oidc) AddKeyPair(keyID, publicKey, privateKey string)

AddKeyPair add a key pair

func (*Oidc) Auth

func (oidc *Oidc) Auth(
	user *AuthUser,
	responseType,
	clientID,
	redirectURI,
	scope,
	state string,
) (*OidcCode, error)

Auth build a auth code for user on the client

func (*Oidc) CheckClient

func (oidc *Oidc) CheckClient(appID, secret string) bool

CheckClient check the client id and secret match or not

func (*Oidc) FindClient

func (oidc *Oidc) FindClient(clientID string) (*OidcClient, bool)

FindClient find oidc client by client id

func (*Oidc) FindUser

func (oidc *Oidc) FindUser(token string) (*OidcUser, error)

FindUser find user by access token

func (*Oidc) GetKeys

func (oidc *Oidc) GetKeys() *Oidc

GetKeys get the jwks

Here we use Oidc entity for jwks, cause of the property Keys is the only exported one.

func (*Oidc) GrantToken

func (oidc *Oidc) GrantToken(code string) (*OidcToken, error)

GrantToken grant the auth code a token

func (*Oidc) RefreshToken

func (oidc *Oidc) RefreshToken(token string) (*OidcToken, error)

RefreshToken refresh the token with refresh token

type OidcClient

type OidcClient struct {
	ClientID    string `json:"client_id"`
	ClientDesc  string `json:"client_desc"`
	RedirectURI string `json:"redirect_uri"`
}

OidcClient the oidc client entity

User should register the oidc client first.

The oidc client data saved in db.

type OidcCode

type OidcCode struct {
	Code         string `json:"code"`
	State        string `json:"state"`
	IDToken      string `json:"id_token,omitempty"`
	AccessToken  string `json:"access_token,omitempty"`
	TokenType    string `json:"token_type,omitempty"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	Openid       string `json:"openid,omitempty"`
}

OidcCode oidc code entity

type OidcToken

type OidcToken struct {
	AccessToken  string `json:"access_token"`
	TokenType    string `json:"token_type"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	Openid       string `json:"openid"`
}

OidcToken oidc token entity

type OidcUser

type OidcUser struct {
	Username string `json:"username"`
	Openid   string `json:"openid"`
}

OidcUser oidc userinfo entity

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server Http server

func NewServer

func NewServer(
	addr, staticPath string,
	db DB,
	log Logger,
	oidc *Oidc,
	auth *Auth,
	kerberos *Kerberos,
) *Server

NewServer Create a Server entity

func (*Server) Run

func (s *Server) Run(certFile, keyFile string)

Run Run http server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL