diff --git a/docs/authentication.md b/docs/authentication.md index 56e2e86..0f8acfe 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -52,12 +52,3 @@ func (a *Authenticator) GetToken() (string, error) { } ``` -## Error Handling - -Errors in the Authentication module are represented using the `olError` package, and they can be of the following types: - -- `AuthenticationError`: Represents an error that occurred during the authentication process, e.g., missing environment variables, failed authentication request, failed token generation or revocation. -- `RequestError`: Represents an error that occurred while creating or sending an HTTP request. -- `SerializationError`: Represents an error that occurred while marshalling or unmarshalling JSON data. - -Each error type is associated with a specific error message that provides more details about the error. diff --git a/docs/error_handling.md b/docs/error_handling.md index e69de29..bea523b 100644 --- a/docs/error_handling.md +++ b/docs/error_handling.md @@ -0,0 +1,33 @@ +# Error Handling + +1. AuthenticationError: + - Purpose: Represents an error related to authentication. + - Fields: + - Message: Provides additional information about the error. + +2. APIError: + - Purpose: Represents an error related to API calls. + - Fields: + - Message: Provides additional information about the error. + - Code: Specifies the error code associated with the API error. + +3. SerializationError: + - Purpose: Represents an error related to serialization. + - Fields: + - Message: Provides additional information about the error. + +4. SDKError: + - Purpose: Represents a general SDK error. + - Fields: + - Message: Provides additional information about the error. + +5. RequestError: + - Purpose: Represents an error related to making an authentication request. + - Fields: + - Message: Provides additional information about the error. + +Each error type has an associated Error() method that returns a formatted error message based on the error type and the provided error message. Additionally, there are corresponding New functions that create and return an error instance with the specified error message. + +To use these error types, you can import the `error` package and utilize the respective New functions to create specific error instances when necessary. + +Please note that it's important to handle and propagate errors appropriately in your code to ensure proper error handling and debugging. \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 44c1f2a..a65b572 100644 --- a/docs/index.md +++ b/docs/index.md @@ -20,7 +20,6 @@ This documentation provides detailed information on how to use the SDK to intera The SDK follows a modular structure with the following key components: -- `cmd/main.go`: Entry point of the SDK application. - `internal/api`: Contains the implementation of the API client, request handling, and response parsing. - `internal/authentication`: Provides authentication mechanisms and related functionality. - `internal/error`: Defines various error types used by the SDK. diff --git a/docs/models.md b/docs/models.md index 3b9e282..df362af 100644 --- a/docs/models.md +++ b/docs/models.md @@ -1,6 +1,6 @@ # SDK Models -The SDK provides several models that are used for data representation and interaction with the API. These models define the structure of the data that can be sent or received when using the SDK methods. +The SDK provides several models that define the structure of data used for data representation and interaction with the OneLogin API. These models are utilized in the SDK's client implementation to handle requests to the OneLogin API. The `Client` struct provides methods such as `Get`, `Delete`,`Post`, and `Put` that internally use the specified models to serialize data into JSON and send HTTP requests to the API. These methods handle authentication, path construction, query parameters, and request body serialization based on the provided models. The following is a list of models available in the SDK: @@ -10,7 +10,7 @@ The `App` model represents an application within the OneLogin platform. It conta ```go type App struct { - ID int64 `json:"id"` + ID int64 `json:"id,omitempty"` Name string `json:"name"` Description string `json:"description,omitempty"` // ... diff --git a/docs/usage_examples.md b/docs/usage_examples.md index e69de29..edddc1b 100644 --- a/docs/usage_examples.md +++ b/docs/usage_examples.md @@ -0,0 +1,52 @@ +```go +package main + +import ( + "fmt" + + "github.com/onelogin/onelogin-go-sdk/internal/models" + "github.com/onelogin/onelogin-go-sdk/pkg/onelogin" +) + +func main() { + var testSSO = models.SSOOpenId{ClientID: "1234567890"} + var testConfig = models.ConfigurationOpenId{ + RedirectURI: "https://example.com", + LoginURL: "https://example.com/login", + OidcApplicationType: 1, + TokenEndpointAuthMethod: 1, + AccessTokenExpirationMinutes: 60, + RefreshTokenExpirationMinutes: 60, + } + + var ( + connetorid int32 = 108419 + name string = "Bow wow wow yippy yo yippy yay" + descr string = "Dog app" + ) + + client, err := onelogin.NewOneloginSDK() + if err != nil { + fmt.Println(err) + } + appQ := models.AppQuery{} + applist, err := client.GetApps(&appQ) + if err != nil { + fmt.Println(err) + } + fmt.Printf("%+v\n", applist) + + appT, err := client.CreateApp(models.App{ + ConnectorID: &connetorid, + Name: &name, + Description: &descr, + SSO: testSSO, + Configuration: testConfig, + }) + if err != nil { + fmt.Println(err) + } + fmt.Printf("%+v\n", appT) +} + +``` \ No newline at end of file diff --git a/internal/api/client.go b/internal/api/client.go index 220bca3..4efc595 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -124,18 +124,13 @@ func (c *Client) DeleteWithBody(path *string, body interface{}) (*http.Response, // Post sends a POST request to the specified path with the given query parameters and request body. func (c *Client) Post(path *string, body interface{}) (*http.Response, error) { - var bodyReader io.Reader - - if body != nil { - // Convert request body to JSON - jsonBody, err := json.Marshal(body) - if err != nil { - return nil, err - } - bodyReader = bytes.NewReader(jsonBody) + // Convert request body to JSON + jsonBody, err := json.Marshal(body) + if err != nil { + return nil, err } - req, err := c.newRequest(http.MethodPost, path, nil, bodyReader) + req, err := c.newRequest(http.MethodPost, path, nil, bytes.NewReader(jsonBody)) if err != nil { return nil, err } diff --git a/internal/error/network_error.go b/internal/error/network_error.go deleted file mode 100644 index 0e7cfc8..0000000 --- a/internal/error/network_error.go +++ /dev/null @@ -1,6 +0,0 @@ -package error - -type NetworkError struct { - Message string - Code string -} diff --git a/internal/error/validation_error.go b/internal/error/validation_error.go deleted file mode 100644 index bc0ba40..0000000 --- a/internal/error/validation_error.go +++ /dev/null @@ -1,6 +0,0 @@ -package error - -type ValidationError struct { - Message string - Code string -} diff --git a/internal/models/app.go b/internal/models/app.go index f98ecdf..1cf01c0 100644 --- a/internal/models/app.go +++ b/internal/models/app.go @@ -1,9 +1,9 @@ package models type App struct { - ID int32 `json:"id,omitempty"` - ConnectorID int32 `json:"connector_id"` - Name string `json:"name"` + ID *int32 `json:"id,omitempty"` + ConnectorID *int32 `json:"connector_id"` + Name *string `json:"name"` Description *string `json:"description,omitempty"` Notes *string `json:"notes,omitempty"` PolicyID *int `json:"policy_id,omitempty"` @@ -17,8 +17,8 @@ type App struct { RoleIDs *[]int `json:"role_ids,omitempty"` AllowAssumedSignin *bool `json:"allow_assumed_signin,omitempty"` Provisioning *Provisioning `json:"provisioning,omitempty"` - SSO *interface{} `json:"sso,omitempty"` - Configuration *interface{} `json:"configuration,omitempty"` + SSO interface{} `json:"sso,omitempty"` + Configuration interface{} `json:"configuration,omitempty"` Parameters *map[string]Parameter `json:"parameters,omitempty"` EnforcementPoint *EnforcementPoint `json:"enforcement_point,omitempty"` } @@ -27,6 +27,10 @@ type Provisioning struct { Enabled bool `json:"enabled"` } +type SSO interface { + ValidateSSO() error +} + type SSOOpenId struct { ClientID string `json:"client_id"` } diff --git a/internal/models/app_rule.go b/internal/models/app_rule.go index ef7db33..9ab8e41 100644 --- a/internal/models/app_rule.go +++ b/internal/models/app_rule.go @@ -25,9 +25,9 @@ type AppRule struct { } type AppRuleQuery struct { - Limit string - Page string - Cursor string + Limit string `json:"limit,omitempty"` + Page string `json:"page,omitempty"` + Cursor string `json:"cursor,omitempty"` Enabled bool `json:"enabled,omitempty"` HasCondition *string `json:"has_condition,omitempty"` HasConditionType *string `json:"has_condition_type,omitempty"` diff --git a/internal/models/privilege.go b/internal/models/privilege.go index 2abf97f..a6960b7 100644 --- a/internal/models/privilege.go +++ b/internal/models/privilege.go @@ -2,9 +2,9 @@ package models // PrivilegeQuery represents available query parameters type PrivilegeQuery struct { - Limit string - Page string - Cursor string + Limit string `json:"limit,omitempty"` + Page string `json:"page,omitempty"` + Cursor string `json:"cursor,omitempty"` } // Privilege represents the Role resource in OneLogin diff --git a/internal/models/role.go b/internal/models/role.go index 1958c6e..533faaa 100644 --- a/internal/models/role.go +++ b/internal/models/role.go @@ -2,9 +2,9 @@ package models // RoleQuery represents available query parameters type RoleQuery struct { - Limit string - Page string - Cursor string + Limit string `json:"limit,omitempty"` + Page string `json:"page,omitempty"` + Cursor string `json:"cursor,omitempty"` } // Role represents the Role resource in OneLogin diff --git a/internal/models/smart_hook.go b/internal/models/smart_hook.go index ad5ed25..6f04246 100644 --- a/internal/models/smart_hook.go +++ b/internal/models/smart_hook.go @@ -28,10 +28,10 @@ const ( // SmartHookQuery represents available query parameters type SmartHookQuery struct { - Limit string - Page string - Cursor string - Type string + Limit string `json:"limit,omitempty"` + Page string `json:"page,omitempty"` + Cursor string `json:"cursor,omitempty"` + Type string `json:"type,omitempty"` } // SmartHook represents a OneLogin SmartHook with associated resource data diff --git a/pkg/onelogin/apps.go b/pkg/onelogin/apps.go index f0c1287..5fd4cbb 100644 --- a/pkg/onelogin/apps.go +++ b/pkg/onelogin/apps.go @@ -9,7 +9,7 @@ const ( AppPath string = "api/2/apps" ) -func (sdk *OneloginSDK) CreateApp(app *mod.App) (interface{}, error) { +func (sdk *OneloginSDK) CreateApp(app mod.App) (interface{}, error) { p, err := utl.BuildAPIPath(AppPath) if err != nil { return nil, err @@ -74,7 +74,7 @@ func (sdk *OneloginSDK) DeleteApp(id int) (interface{}, error) { } -func (sdk *OneloginSDK) CreateAppRule(id int, appRule *mod.AppRule) (interface{}, error) { +func (sdk *OneloginSDK) CreateAppRule(id int, appRule mod.AppRule) (interface{}, error) { p, err := utl.BuildAPIPath(AppPath, id) if err != nil { return nil, err diff --git a/pkg/onelogin/roles.go b/pkg/onelogin/roles.go index 6209bee..05d5d1c 100644 --- a/pkg/onelogin/roles.go +++ b/pkg/onelogin/roles.go @@ -10,7 +10,10 @@ var ( ) func (sdk *OneloginSDK) CreateRole(role *mod.Role) (interface{}, error) { - p := RolePath + p, err := utl.BuildAPIPath(RolePath) + if err != nil { + return nil, err + } resp, err := sdk.Client.Post(&p, role) if err != nil { return nil, err diff --git a/pkg/onelogin/users.go b/pkg/onelogin/users.go index e615803..a3cd93c 100644 --- a/pkg/onelogin/users.go +++ b/pkg/onelogin/users.go @@ -16,7 +16,11 @@ const ( // Users V2 func (sdk *OneloginSDK) CreateUser(user mod.User) (interface{}, error) { - p := UserPathV2 + p, err := utl.BuildAPIPath(UserPathV2) + if err != nil { + return nil, err + } + resp, err := sdk.Client.Post(&p, user) if err != nil { return nil, err diff --git a/tests/error_handling_test.go b/tests/error_handling_test.go index 4c4c53b..adeb9bd 100644 --- a/tests/error_handling_test.go +++ b/tests/error_handling_test.go @@ -7,31 +7,6 @@ import ( "github.com/onelogin/onelogin-go-sdk/internal/error" ) -func TestValidationError(t *testing.T) { - expectedMessage := "mock message" - expectedCode := "mock code" - validationError := error.ValidationError{ - Message: expectedMessage, - Code: expectedCode, - } - - t.Run("when Message is accessed", func(t *testing.T) { - message := validationError.Message - - if message != expectedMessage { - t.Errorf("unexpected message: got %q, want %q", message, expectedMessage) - } - }) - - t.Run("when Code is accessed", func(t *testing.T) { - code := validationError.Code - - if code != expectedCode { - t.Errorf("unexpected code: got %q, want %q", code, expectedCode) - } - }) -} - func TestNewSerializationError(t *testing.T) { expectedMessage := "mock message" err := error.NewSerializationError(expectedMessage)