Add API for resolver
This commit is contained in:
parent
4f4fa2b3d7
commit
3ef9c23a0c
7 changed files with 98 additions and 7 deletions
3
API.md
3
API.md
|
|
@ -18,8 +18,9 @@ NOTE: These routes will be documented using a Swagger / OpenAPI definition in th
|
||||||
All routes can be found under `/api/v1/http/`
|
All routes can be found under `/api/v1/http/`
|
||||||
|
|
||||||
- `/api/v1/auth`: Returns api session information
|
- `/api/v1/auth`: Returns api session information
|
||||||
- `/api/v1/systems`: Returns a (publically visible) list of systems
|
|
||||||
- `/api/v1/news`: Returns JSON containing all news items
|
- `/api/v1/news`: Returns JSON containing all news items
|
||||||
|
- `/api/v1/instances/directory`: Returns a (publically visible) list of systems
|
||||||
|
- `/api/v1/resolve?uri=...`: Resolve a URI
|
||||||
|
|
||||||
|
|
||||||
## Interactive Websocket API
|
## Interactive Websocket API
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,6 @@ func (*ListInstancesScope) Scope() component.ScopeInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lis *ListInstancesScope) HasScope(param string, r *http.Request) (bool, error) {
|
func (lis *ListInstancesScope) HasScope(param string, r *http.Request) (bool, error) {
|
||||||
// TODO: at the moment everyone has this permission
|
_, user, err := lis.Dependencies.Auth.SessionOf(r)
|
||||||
// this should change in the future!
|
return user != nil, err
|
||||||
return true, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,6 @@ func (*ListNewsScope) Scope() component.ScopeInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lns *ListNewsScope) HasScope(param string, r *http.Request) (bool, error) {
|
func (lns *ListNewsScope) HasScope(param string, r *http.Request) (bool, error) {
|
||||||
// TODO: at the moment everyone has this permission
|
_, user, err := lns.Dependencies.Auth.SessionOf(r)
|
||||||
return true, nil
|
return user != nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
37
internal/dis/component/auth/scopes/resolver.go
Normal file
37
internal/dis/component/auth/scopes/resolver.go
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
package scopes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ResolverScope struct {
|
||||||
|
component.Base
|
||||||
|
Dependencies struct {
|
||||||
|
Auth *auth.Auth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ component.ScopeProvider = (*ResolverScope)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ScopeResolver Scope = "url.resolve"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (*ResolverScope) Scope() component.ScopeInfo {
|
||||||
|
return component.ScopeInfo{
|
||||||
|
Scope: ScopeResolver,
|
||||||
|
Description: "resolve a URI to a URL to display it in",
|
||||||
|
DeniedMessage: "",
|
||||||
|
TakesParam: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rs *ResolverScope) HasScope(param string, r *http.Request) (bool, error) {
|
||||||
|
_, user, err := rs.Dependencies.Auth.SessionOf(r)
|
||||||
|
return user != nil, err
|
||||||
|
}
|
||||||
53
internal/dis/component/resolver/api.go
Normal file
53
internal/dis/component/resolver/api.go
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
package resolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/api"
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
|
||||||
|
"github.com/tkw1536/pkglib/httpx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type API struct {
|
||||||
|
component.Base
|
||||||
|
Dependencies struct {
|
||||||
|
Auth *auth.Auth
|
||||||
|
Resolver *Resolver
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ component.Routeable = (*API)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
func (api *API) Routes() component.Routes {
|
||||||
|
return component.Routes{
|
||||||
|
Prefix: "/api/v1/resolve/",
|
||||||
|
Exact: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
|
||||||
|
return &api.Handler[string]{
|
||||||
|
Config: a.Config,
|
||||||
|
Auth: a.Dependencies.Auth,
|
||||||
|
|
||||||
|
Methods: []string{"GET"},
|
||||||
|
|
||||||
|
Scope: scopes.ScopeResolver,
|
||||||
|
Handler: func(s string, r *http.Request) (string, error) {
|
||||||
|
uri := r.URL.Query().Get("uri")
|
||||||
|
if uri == "" {
|
||||||
|
return "", httpx.ErrBadRequest
|
||||||
|
}
|
||||||
|
target := a.Dependencies.Resolver.Target(uri)
|
||||||
|
if target == "" {
|
||||||
|
return "", httpx.ErrNotFound
|
||||||
|
}
|
||||||
|
return target, nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
@ -63,7 +63,6 @@ type ScopeProvider interface {
|
||||||
|
|
||||||
// Check checks if the given session has access to the given scope.
|
// Check checks if the given session has access to the given scope.
|
||||||
HasScope(param string, r *http.Request) (bool, error)
|
HasScope(param string, r *http.Request) (bool, error)
|
||||||
// TODO: move this to a session
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionInfo provides information about the current session.
|
// SessionInfo provides information about the current session.
|
||||||
|
|
|
||||||
|
|
@ -154,6 +154,7 @@ func (dis *Distillery) allComponents() []initFunc {
|
||||||
auto[*scopes.AdminLoggedIn],
|
auto[*scopes.AdminLoggedIn],
|
||||||
auto[*scopes.ListInstancesScope],
|
auto[*scopes.ListInstancesScope],
|
||||||
auto[*scopes.ListNewsScope],
|
auto[*scopes.ListNewsScope],
|
||||||
|
auto[*scopes.ResolverScope],
|
||||||
|
|
||||||
// instances
|
// instances
|
||||||
auto[*instances.Instances],
|
auto[*instances.Instances],
|
||||||
|
|
@ -203,5 +204,6 @@ func (dis *Distillery) allComponents() []initFunc {
|
||||||
auto[*list.API],
|
auto[*list.API],
|
||||||
auto[*list.API],
|
auto[*list.API],
|
||||||
auto[*news.API],
|
auto[*news.API],
|
||||||
|
auto[*resolver.API],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue