-
Notifications
You must be signed in to change notification settings - Fork 9
Advanced
This is useful for custom authorization or general need to access the HTTP request information
Steps:
1.) Register a request restconf.RequestFilter
with RESTCONF restconf.Server
instance
2.) Filter returns a context.Context
that contains any custom data that you might extract from the HTTP request like HTTP header information, URL parameters or certificate information.
3.) Values from that context.Context will be made available to all your node.Node
implementations
Example Code:
s := restconf.NewServer(d)
s.Filters = append(s.Filters, filter)
func filter(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, error) {
return context.WithValue(ctx, "mykey", "my-value"), nil
}
func myNode() node.Node {
return nodeutil.Basic{
OnAction: func(r node.ActionRequest) (node.Node, error) {
myval := r.Selection.Context.Value("mykey")
}
}
}
There are a number of breaking changes in FreeCONF yang and restconf libraries needed to be made to be in compliance with RFC. This is of course important so FreeCONF can be used with other libraries. In order to not break existing codebases, there are two data structures that you can use to restore previous operations until you have time to port your code over to the stricter compliance. Eventually this legacy mode will be removed.
restconf.Compliance = restconf.Simplistic
yang.Compliance = yang.Simplistic
When coding your node.Node
implementation, if you return an error
from any function, your http clients will receive a 500
error, but if you want to return an error with a different HTTP status code, you can use or wrap one of the predefined errors
import (
"github.com/freeconf/yang/fc"
)
...
// Option #1 - straight 401 error
return fc.UnauthorizedError
// Option #2 - enhanced but still an 401 error
return fmt.Errorf("Bad ACL %w", fc.UnauthorizedError)