Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmfwolf committed Jul 11, 2023
1 parent 1225b0d commit 980106f
Show file tree
Hide file tree
Showing 49 changed files with 3,623 additions and 0 deletions.
3 changes: 3 additions & 0 deletions v4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
./vendor
coverage.out
bin
22 changes: 22 additions & 0 deletions v4/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2010-2023 OneLogin, Inc.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions v4/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
run:
go run './cmd/main.go'

build:
go build './...'

test:
go test -v ./tests/...

secure:
# or install it into ./bin/
curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s
./bin/gosec -exclude=G104 ./...

link:
ln -s ${GOPATH}/src/github.com/onelogin/onelogin-go-sdk .
1 change: 1 addition & 0 deletions v4/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
V4.0.0 is a new implementation and is not compatible with previous versions of the SDK
1 change: 1 addition & 0 deletions v4/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version: "4.0.1"
85 changes: 85 additions & 0 deletions v4/docs/Design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# SDK Design Document

## Introduction

This design document outlines the architecture and implementation approach for building a new SDK for Onelogin's API. The SDK aims to provide developers with a convenient and user-friendly interface to interact with Onelogin's services.

## Goals

- Modularity: Design the SDK with separate modules for authentication, API request handling, data models, error handling, and utility functions.
- Encapsulation: Define well-defined interfaces for each module to encapsulate their functionality and hide implementation details.
- Simplified Interface: Use the Facade pattern to provide a unified and simplified interface for users of the SDK, shielding them from underlying complexities.
- Customizability: Implement dependency injection to allow users to customize or replace certain components, such as authentication or HTTP client implementations.

## Architecture Overview

The SDK architecture will be based on a combination of Modular Architecture, Facade Pattern, and Dependency Injection.

### Modular Architecture

Identify the following functional components for the SDK:

1. Authentication: Responsible for handling authentication mechanisms.
2. API: Handles API request construction, communication, and response handling.
3. Models: Represents entities and resources in the Onelogin API.
4. Error Handling: Manages errors and provides error types and codes.
5. Utilities: Contains utility functions and helper methods.

Create separate modules or packages for each component to ensure clear separation of concerns and better organization.

### Facade Pattern

Design a facade module or package, "onelogin," that acts as the entry point for SDK users. The facade will provide a simplified interface and shield users from the complexities of underlying modules.

The facade will offer methods for authentication, making API requests, handling responses, and error management. It will internally coordinate and interact with the authentication and API modules to fulfill these functionalities.

### Dependency Injection

Identify components within the SDK that can benefit from customization or replacement by users, such as the authentication module or the HTTP client implementation.

Design interfaces for these components, such as "Authenticator" and "HTTPClient," specifying the required methods or functionality. Implement default implementations for these interfaces, which will be used if users do not provide custom implementations.

Allow users to provide their custom implementations of these interfaces by accepting them as parameters in relevant methods or constructors. Use dependency injection to inject the appropriate implementation instances into the modules or components that require them.

## Implementation Steps

1. Set up the project structure with separate directories for each module: authentication, api, models, error, and utilities.
2. Implement the authentication module:
- Create an "auth.go" file within the authentication package to handle authentication mechanisms (e.g., API key, OAuth, or SAML).
- Implement methods to authenticate and generate authentication tokens.
3. Implement the API module:
- Create a "client.go" file within the api package to handle API request construction, communication, and response handling.
- Implement methods for making HTTP requests, handling response parsing, and error handling.
4. Implement the models module:
- Create files (e.g., "user.go" and "group.go") within the models package to define the data models that represent Onelogin entities and resources.
- Define appropriate struct types, fields, and methods for convenient data access and manipulation.
5. Implement the error handling module:
- Create an "error.go" file within the error package to define error types and codes that reflect possible failures and error scenarios in the Onelogin API.
- Implement error handling mechanisms that allow users to handle and recover from errors effectively.
6. Implement the utilities module:
- Create a "util.go" file within the utilities package to include utility functions and helper methods used across the SDK.
- Implement commonly used functions, such as data serialization, date/time formatting, or string manipulation.

7. Design and implement the facade module:
- Create an "onelogin.go" file within the onelogin package to define the facade module.
- Implement methods that provide a simplified interface for users, delegating calls to the relevant modules.
- Coordinate authentication and API request handling using the appropriate modules.
8. Apply dependency injection:
- Identify components that can be customized or replaced by users, such as the authentication module or the HTTP client implementation.
- Design interfaces (e.g., "Authenticator" and "HTTPClient") within their respective modules and implement default implementations.
- Allow users to provide their custom implementations by accepting them as parameters in relevant methods or constructors.
- Use dependency injection to inject the appropriate implementation instances into the modules or components that require them.
9. Write comprehensive documentation:
- Create documentation files (e.g., index.md, authentication.md, api.md, models.md, error_handling.md, usage_examples.md) within the docs directory.
- Provide clear explanations of the SDK's features, functionalities, and usage examples.
- Include code samples and guidelines to help users integrate and utilize the SDK efficiently.
- Document any customization options and how to use dependency injection.
10. Conduct thorough testing:
- Write unit tests for each module, covering different functionalities and edge cases.
- Create test files (e.g., authentication_test.go, api_test.go, models_test.go, error_handling_test.go) within the tests directory.
- Ensure that the SDK behaves as expected and handles errors appropriately.
11. Continuously iterate and gather feedback from users, making improvements and addressing any issues that arise.

## Conclusion

By following this architecture pattern, we aim to develop a modular, user-friendly SDK for Onelogin's API. The combination of Modular Architecture, Facade Pattern, and Dependency Injection will ensure maintainability, extensibility, and ease of use for developers integrating with Onelogin.
38 changes: 38 additions & 0 deletions v4/docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# API Module Documentation

The API module is a crucial part of the OneLogin Go SDK. It is responsible for constructing and sending HTTP requests to the OneLogin API, and processing the responses. This document will explain the main components of the API module and their role in the SDK.

## Client

The `Client` struct is the primary interface for interacting with the OneLogin API. It uses an `HttpClient` to make HTTP requests, an `Auth` object to handle authentication, and the `OLdomain` (OneLogin domain) for routing the requests.

### `NewClient` Function

The `NewClient` function creates a new `Client` and initializes it with the `http.DefaultClient` as the `HttpClient`, a new `Authenticator` for `Auth`, and sets the `OLdomain` from the `ONELOGIN_SUBDOMAIN` environment variable. This function is typically called at the start of the program to instantiate the client, which is then used to make API calls.

### `newRequest` Function

This function creates a new HTTP request with the specified method, path, query parameters, and request body. It is a helper function, used by the HTTP methods (Get, Post, Delete, Put) of the `Client` to construct a new request. The function takes the method type (GET, POST, etc.), the API path, an object for query parameters, and a body for the request, and returns an HTTP request that is ready to be sent.

### `sendRequest` Function

This function sends an HTTP request and returns the HTTP response. It is used by the HTTP methods (Get, Post, Delete, Put) of the `Client` to send requests. This function also checks the response status code, and if it detects a `http.StatusUnauthorized` (HTTP 401), it attempts to refresh the token and retry the request.

## HTTP Methods

The `Client` struct provides the following methods for making HTTP requests:

- `Get`: Makes a GET request to the specified path with optional query parameters. It is used to retrieve information from the OneLogin API.
- `Post`: Makes a POST request to the specified path. It sends data to the OneLogin API to create a new resource.
- `Delete`: Makes a DELETE request to the specified path. It is used to delete a resource from the OneLogin API.
- `Put`: Makes a PUT request to the specified path. It is used to update a resource in the OneLogin API.

Each of these methods uses the `newRequest` function to create the HTTP request, and the `sendRequest` function to send the request and retrieve the response. These methods make the process of interacting with the OneLogin API simpler and more intuitive.

## Authenticator

The `Authenticator` interface is used for handling authentication. It uses the `GetToken` method for retrieving authentication tokens. The tokens are needed for authenticating requests to the OneLogin API.

The `Authenticator` is initialized with the `NewAuthenticator` function and the token is generated with the `GenerateToken` method within the `NewClient` function. If a request is unauthorized (HTTP 401), the token is refreshed using the `GenerateToken` method in the `sendRequest` function.

In summary, the API module simplifies the process of interacting with the OneLogin API by encapsulating the details of creating, sending, and processing HTTP requests. It uses environment variables for the API credentials and handles error scenarios such as unauthorized requests and token refresh. It forms the backbone of the OneLogin Go SDK, providing a streamlined interface for making API calls.
54 changes: 54 additions & 0 deletions v4/docs/authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Authentication Module Documentation

The Authentication module provides functionalities to handle the authentication process with the OneLogin API in the Go SDK. This process includes generating and revoking access tokens, which are necessary for making authenticated requests to the API. The module is implemented in the `authentication.go` file in the `authentication` package.

## Authenticator Struct

The `Authenticator` struct represents an authenticator object that can handle authentication processes. It holds the `accessToken` as a field. The `accessToken` field stores the generated access token used for making authenticated API calls.

```go
type Authenticator struct {
accessToken string
}
```

## NewAuthenticator Function

The `NewAuthenticator` function is used to create a new `Authenticator` instance. It does not require any arguments and it initializes a new Authenticator with an empty `accessToken`.

```go
func NewAuthenticator() *Authenticator {
return &Authenticator{}
}
```

## GenerateToken Function

The `GenerateToken` function is used to generate a new access token. It reads the `ONELOGIN_CLIENT_ID` and `ONELOGIN_CLIENT_SECRET` environment variables, creates an authentication request, sends it, and handles the response. The newly generated access token is stored in the `Authenticator` instance.

```go
func (a *Authenticator) GenerateToken() error {
// implementation details
}
```

## RevokeToken Function

The `RevokeToken` function is used to revoke an existing access token. It reads the `ONELOGIN_CLIENT_ID` and `ONELOGIN_CLIENT_SECRET` environment variables, creates a revocation request, sends it, and handles the response. If the revocation is successful, a confirmation message is printed.

```go
func (a *Authenticator) RevokeToken(token, domain *string) error {
// implementation details
}
```

## GetToken Function

The `GetToken` function is used to retrieve the current access token from the `Authenticator` instance. It returns the `accessToken` field of the `Authenticator` struct.

```go
func (a *Authenticator) GetToken() (string, error) {
return a.accessToken, nil
}
```

33 changes: 33 additions & 0 deletions v4/docs/error_handling.md
Original file line number Diff line number Diff line change
@@ -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<ErrorType> 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<ErrorType> 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.
32 changes: 32 additions & 0 deletions v4/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Onelogin Go SDK Documentation

This documentation provides detailed information on how to use the SDK to interact with the OneLogin platform.

## Table of Contents

- [API Reference](api.md): Learn about the available API endpoints and how to make requests using the SDK.
- [Authentication](authentication.md): Understand the authentication mechanisms supported by the SDK and how to authenticate your requests.
- [Error Handling](error_handling.md): Explore the different types of errors that can occur during SDK usage and how to handle them properly.
- [SDK Models](models.md): Get familiar with the models used by the SDK to represent data and interact with the API.
- [Usage Examples](usage_examples.md): Find example code snippets and scenarios to help you understand how to use the SDK effectively.

## Additional Resources

- [Design](Design.md): Dive into the design considerations and architecture of the SDK.
- [License](../LICENSE): View the license agreement for the SDK.
- [README](../README.md): Read the SDK's introductory information, installation instructions, and basic usage guidelines.

## Code Structure

The SDK follows a modular structure with the following key components:

- `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.
- `internal/models`: Contains the model definitions used to represent data exchanged with the API.
- `internal/utilities`: Includes utility functions and helper methods.
- `pkg/onelogin`: Contains the main implementation of the OneLogin SDK.

## Getting Started

To get started with the SDK, follow the installation instructions outlined in the [README](../README.md) file. Once the SDK is installed, refer to the usage examples in [Usage Examples](usage_examples.md) to understand how to use the SDK to perform various operations.
Loading

0 comments on commit 980106f

Please sign in to comment.