-
Notifications
You must be signed in to change notification settings - Fork 1
/
securesite.go
87 lines (72 loc) · 2.81 KB
/
securesite.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Package secureconfig provides the plugin with a secure API to interact with Ambient.
package secureconfig
import (
"net/http"
"github.com/ambientkit/ambient"
"github.com/ambientkit/ambient/internal/pluginsafe"
"github.com/ambientkit/ambient/pkg/amberror"
)
//go:generate go run github.com/vburenin/ifacemaker -f *.go -s SecureSite -i SecureSite -p ambient -o ../../gen_securesite.go -y "SecureSite provides plugin functions." -c "Code generated by ifacemaker. DO NOT EDIT."
// SecureSite is a secure data access for the site.
type SecureSite struct {
pluginName string
log ambient.AppLogger
pluginsystem ambient.PluginSystem
sess ambient.AppSession
mux ambient.AppRouter
render ambient.Renderer
recorder *pluginsafe.RouteRecorder
}
// NewSecureSite returns a new secure site.
func NewSecureSite(pluginName string, log ambient.AppLogger, ps ambient.PluginSystem,
session ambient.AppSession, mux ambient.AppRouter, render ambient.Renderer, recorder *pluginsafe.RouteRecorder, loadPlugins bool) (*SecureSite, http.Handler, error) {
ss := &SecureSite{
pluginName: pluginName,
log: log,
sess: session,
mux: mux,
pluginsystem: ps,
render: render,
recorder: recorder,
}
if loadPlugins {
err := ss.loadAllPluginPages()
if err != nil {
return nil, nil, err
}
return ss, ss.loadAllPluginMiddleware(), nil
}
return ss, nil, nil
}
// Error returns the proper error. Separated to allow reuse for gRPC.
func Error(siteError error) (err error) {
switch siteError {
case amberror.ErrAccessDenied, amberror.ErrGrantNotRequested, amberror.ErrSettingNotSpecified:
return ambient.StatusError{Code: http.StatusForbidden, Err: siteError}
case amberror.ErrNotFound:
return ambient.StatusError{Code: http.StatusNotFound, Err: siteError}
default:
// switch strings.TrimSuffix(siteError.Error(), "\n") { // FIXME: Need to get this to work.
// case amberror.ErrAccessDenied.Error(), amberror.ErrGrantNotRequested.Error(), amberror.ErrSettingNotSpecified.Error():
// return ambient.StatusError{Code: http.StatusForbidden, Err: siteError}
// case amberror.ErrNotFound.Error():
// return ambient.StatusError{Code: http.StatusNotFound, Err: siteError}
// }
return ambient.StatusError{Code: http.StatusInternalServerError, Err: siteError}
}
}
// Error handles returning the proper error.
func (ss *SecureSite) Error(siteError error) (err error) {
return Error(siteError)
}
// Load forces a reload of the data.
func (ss *SecureSite) Load() error {
if !ss.Authorized(ambient.GrantSiteLoadTrigger) {
return amberror.ErrAccessDenied
}
return ss.pluginsystem.Load()
}
// Authorized determines if the current context has access.
func (ss *SecureSite) Authorized(grant ambient.Grant) bool {
return ss.pluginsystem.Authorized(ss.pluginName, grant)
}