Skip to content

Commit

Permalink
feat: change server constructor to accept mandatory arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
icamys authored and q-uint committed Mar 15, 2024
1 parent d2ce733 commit f417b10
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 231 deletions.
35 changes: 8 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,35 +119,16 @@ resourceTypes := []ResourceType{
### 4. Create Server

```go
server := NewServer(
WithServiceProviderConfig(config),
WithResourceTypes(resourceTypes),
WithLogger(logger), // optional, default is no logging
)
```

## Logging

No incoming or outgoing (incl. errors) requests are logged by default. It is up to the user to implement this. This can
either be done through middleware around the server or by implementing the `ResourceHandler` interface.

### Internal

The SCIM server uses the standard `slog` package for logging.

There are two moments where the server logs:

1. When it was not able to marshal the response, it will log the error. This should not happen, since these are
predefined structures, of which most have custom `MarshalJSON` methods. In these cases an `errors.ScimErrorInternal`
error is returned.
2. When the server was not able to `Write` the response.
serverArgs := &ServerArgs{
ServiceProviderConfig: config,
ResourceTypes: resourceTypes,
}

This logger can be customized by overwriting the default `slog.Logger`.
serverOpts := []ServerOption{
WithLogger(logger), // optional, default is no logging
}

```go
var scimLogger slog.Logger
// initialize w/ own implementation
scim.SetLogger(scimLogger)
server := NewServer(serverArgs, serverOpts...)
```

## String Values for Attributes
Expand Down
27 changes: 24 additions & 3 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@ import (
)

func ExampleNewServer() {
server := NewServer()
args := &ServerArgs{
ServiceProviderConfig: &ServiceProviderConfig{},
ResourceTypes: []ResourceType{},
}
server, err := NewServer(args)
if err != nil {
logger.Fatal(err)
}
logger.Fatal(http.ListenAndServe(":7643", server))
}

func ExampleNewServer_basePath() {
server := NewServer()
args := &ServerArgs{
ServiceProviderConfig: &ServiceProviderConfig{},
ResourceTypes: []ResourceType{},
}
server, err := NewServer(args)
if err != nil {
logger.Fatal(err)
}
// You can host the SCIM server on a custom path, make sure to strip the prefix, so only `/v2/` is left.
http.Handle("/scim/", http.StripPrefix("/scim", server))
logger.Fatal(http.ListenAndServe(":7643", nil))
Expand All @@ -27,6 +41,13 @@ func ExampleNewServer_logger() {

return http.HandlerFunc(fn)
}
server := NewServer()
args := &ServerArgs{
ServiceProviderConfig: &ServiceProviderConfig{},
ResourceTypes: []ResourceType{},
}
server, err := NewServer(args)
if err != nil {
logger.Fatal(err)
}
logger.Fatal(http.ListenAndServe(":7643", loggingMiddleware(server)))
}
67 changes: 38 additions & 29 deletions filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func Test_Group_Filter(t *testing.T) {
s := newTestServerForFilter()
s := newTestServerForFilter(t)

tests := []struct {
name string
Expand Down Expand Up @@ -72,7 +72,7 @@ func Test_Group_Filter(t *testing.T) {
}

func Test_User_Filter(t *testing.T) {
s := newTestServerForFilter()
s := newTestServerForFilter(t)

tests := []struct {
name string
Expand Down Expand Up @@ -129,37 +129,46 @@ func Test_User_Filter(t *testing.T) {
}
}

func newTestServerForFilter() scim.Server {
return scim.NewServer(
scim.WithResourceTypes([]scim.ResourceType{
{
ID: optional.NewString("User"),
Name: "User",
Endpoint: "/Users",
Description: optional.NewString("User Account"),
Schema: schema.CoreUserSchema(),
Handler: &testResourceHandler{
data: map[string]testData{
"0001": {attributes: map[string]interface{}{"userName": "testUser"}},
"0002": {attributes: map[string]interface{}{"userName": "testUser+test"}},
// newTestServerForFilter creates a new test server with a User and Group resource type
// or fails the test if an error occurs.
func newTestServerForFilter(t *testing.T) scim.Server {
s, err := scim.NewServer(
&scim.ServerArgs{
ServiceProviderConfig: &scim.ServiceProviderConfig{},
ResourceTypes: []scim.ResourceType{
{
ID: optional.NewString("User"),
Name: "User",
Endpoint: "/Users",
Description: optional.NewString("User Account"),
Schema: schema.CoreUserSchema(),
Handler: &testResourceHandler{
data: map[string]testData{
"0001": {attributes: map[string]interface{}{"userName": "testUser"}},
"0002": {attributes: map[string]interface{}{"userName": "testUser+test"}},
},
schema: schema.CoreUserSchema(),
},
schema: schema.CoreUserSchema(),
},
},
{
ID: optional.NewString("Group"),
Name: "Group",
Endpoint: "/Groups",
Description: optional.NewString("Group"),
Schema: schema.CoreGroupSchema(),
Handler: &testResourceHandler{
data: map[string]testData{
"0001": {attributes: map[string]interface{}{"displayName": "testGroup"}},
"0002": {attributes: map[string]interface{}{"displayName": "testGroup+test"}},
{
ID: optional.NewString("Group"),
Name: "Group",
Endpoint: "/Groups",
Description: optional.NewString("Group"),
Schema: schema.CoreGroupSchema(),
Handler: &testResourceHandler{
data: map[string]testData{
"0001": {attributes: map[string]interface{}{"displayName": "testGroup"}},
"0002": {attributes: map[string]interface{}{"displayName": "testGroup+test"}},
},
schema: schema.CoreGroupSchema(),
},
schema: schema.CoreGroupSchema(),
},
},
}),
},
)
if err != nil {
t.Fatal(err)
}
return s
}
Loading

0 comments on commit f417b10

Please sign in to comment.