147 lines
2.9 KiB
Go
147 lines
2.9 KiB
Go
package request
|
|
|
|
import (
|
|
"context"
|
|
"isp-gate-service/domain"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/txix-open/isp-kit/errors"
|
|
)
|
|
|
|
var (
|
|
ErrNotAuthenticated = errors.New("not authenticated")
|
|
)
|
|
|
|
type Context struct {
|
|
request *http.Request
|
|
responseWriter http.ResponseWriter
|
|
|
|
endpointMeta *domain.EndpointMeta
|
|
|
|
authenticated bool
|
|
authData *domain.AppAuthData
|
|
|
|
userAuthenticated bool
|
|
userAuthData *domain.UserAuthData
|
|
|
|
adminAuthenticated bool
|
|
adminId int
|
|
adminToken string
|
|
|
|
queryParams map[string]string
|
|
|
|
httpJsonUtf8Charset bool
|
|
}
|
|
|
|
func NewContext(
|
|
request *http.Request,
|
|
response http.ResponseWriter,
|
|
endpointMeta *domain.EndpointMeta,
|
|
) *Context {
|
|
return &Context{
|
|
request: request,
|
|
responseWriter: response,
|
|
endpointMeta: endpointMeta,
|
|
}
|
|
}
|
|
|
|
func (c *Context) Request() *http.Request {
|
|
return c.request
|
|
}
|
|
|
|
func (c *Context) ResponseWriter() http.ResponseWriter {
|
|
return c.responseWriter
|
|
}
|
|
|
|
func (c *Context) SetResponseWriter(writer http.ResponseWriter) {
|
|
c.responseWriter = writer
|
|
}
|
|
|
|
func (c *Context) EndpointMeta() *domain.EndpointMeta {
|
|
return c.endpointMeta
|
|
}
|
|
|
|
func (c *Context) Authenticate(authData domain.AppAuthData) {
|
|
c.authenticated = true
|
|
c.authData = &authData
|
|
}
|
|
|
|
func (c *Context) GetAuthData() (domain.AppAuthData, error) {
|
|
if !c.authenticated {
|
|
return domain.AppAuthData{}, ErrNotAuthenticated
|
|
}
|
|
return *c.authData, nil
|
|
}
|
|
|
|
func (c *Context) AuthenticateUser(authData domain.UserAuthData) {
|
|
c.userAuthenticated = true
|
|
c.userAuthData = &authData
|
|
}
|
|
|
|
func (c *Context) GetUserAuthData() (domain.UserAuthData, error) {
|
|
if !c.userAuthenticated {
|
|
return domain.UserAuthData{}, ErrNotAuthenticated
|
|
}
|
|
return *c.userAuthData, nil
|
|
}
|
|
|
|
func (c *Context) SkipAppAuth() bool {
|
|
return c.userAuthData != nil && c.userAuthData.SkipAppAuth
|
|
}
|
|
|
|
func (c *Context) IsAdminAuthenticated() bool {
|
|
return c.adminAuthenticated
|
|
}
|
|
|
|
func (c *Context) AdminId() int {
|
|
return c.adminId
|
|
}
|
|
|
|
func (c *Context) AdminToken() string {
|
|
return c.adminToken
|
|
}
|
|
|
|
func (c *Context) AuthenticateAdmin(adminId int, adminToken string) {
|
|
c.adminAuthenticated = true
|
|
c.adminId = adminId
|
|
c.adminToken = adminToken
|
|
}
|
|
|
|
func (c *Context) Context() context.Context {
|
|
return c.request.Context()
|
|
}
|
|
|
|
func (c *Context) SetContext(ctx context.Context) {
|
|
c.request = c.request.WithContext(ctx)
|
|
}
|
|
|
|
func (c *Context) EnableHttpJsonUtf8Charset(enable bool) {
|
|
c.httpJsonUtf8Charset = enable
|
|
}
|
|
|
|
func (c *Context) IsJsonUtf8CharsetEnabled() bool {
|
|
return c.httpJsonUtf8Charset
|
|
}
|
|
|
|
func (c *Context) Param(name string) string {
|
|
value := c.request.Header.Get(name)
|
|
if value != "" {
|
|
return strings.TrimSpace(value)
|
|
}
|
|
|
|
if c.queryParams == nil {
|
|
query := c.request.URL.Query()
|
|
c.queryParams = map[string]string{}
|
|
for key, values := range query {
|
|
if len(values) == 0 {
|
|
continue
|
|
}
|
|
c.queryParams[strings.ToLower(key)] = values[0]
|
|
}
|
|
}
|
|
value = c.queryParams[strings.ToLower(name)]
|
|
|
|
return strings.TrimSpace(value)
|
|
}
|