Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create server instance using constructor, use interface-based logger #170

Merged
merged 6 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,16 @@ resourceTypes := []ResourceType{
### 4. Create Server

```go
server := Server{
Config: config,
serverArgs := &ServerArgs{
ServiceProviderConfig: config,
ResourceTypes: resourceTypes,
}
```

## 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.

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
30 changes: 21 additions & 9 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@ import (
)

func ExampleNewServer() {
server := Server{
Config: ServiceProviderConfig{},
ResourceTypes: nil,
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 := Server{
Config: ServiceProviderConfig{},
ResourceTypes: nil,
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))
Expand All @@ -33,9 +41,13 @@ func ExampleNewServer_logger() {

return http.HandlerFunc(fn)
}
server := Server{
Config: ServiceProviderConfig{},
ResourceTypes: nil,
args := &ServerArgs{
ServiceProviderConfig: &ServiceProviderConfig{},
ResourceTypes: []ResourceType{},
}
server, err := NewServer(args)
if err != nil {
logger.Fatal(err)
}
logger.Fatal(http.ListenAndServe(":7643", loggingMiddleware(server)))
}
65 changes: 37 additions & 28 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.Server{
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"}},
// 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
Loading