Skip to content

Commit

Permalink
updated docs, roles, users, apps, authservers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmfwolf committed Jun 12, 2023
1 parent 8f7d8a7 commit 294258a
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 176 deletions.
9 changes: 2 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@ func main() {
fmt.Println(userList)

appQuery := models.AppQuery{}
appResponse, err := ol.GetApps(&appQuery)
appList, err := ol.GetApps(&appQuery)
if err != nil {
fmt.Println("Failed to get app list:", err)
return
}
appList, ok := appResponse.([]string) // or whatever the correct type should be
if !ok {
fmt.Println("Failed to assert appResponse as type []string")
return
}
fmt.Println(appList)
fmt.Println("App List:", appList)

}
66 changes: 19 additions & 47 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,38 @@
# API Module Documentation

This document describes the use of the API module in the OneLogin Go SDK. The API module provides the basic HTTP methods (GET, POST, DELETE, PUT) for communicating with the OneLogin API, and handles the construction and sending of requests, as well as processing the responses.
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 in the API module is responsible for making requests to the OneLogin API. It contains an `HttpClient` (for making the HTTP requests), an `Auth` (for handling authentication), and the `OLdomain` (the OneLogin domain to which requests are sent).
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.

The `NewClient` function is used to create a new `Client`. It uses the `http.DefaultClient` as the `HttpClient`, creates a new `Authenticator`, and sets the `OLdomain` based on the `ONELOGIN_SUBDOMAIN` environment variable.
### `NewClient` Function

```go
client := api.NewClient()
```
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.

## HTTP Methods

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

- `Get`: This method makes a GET request to the specified path. It takes a `path` and a `queryParams` map as arguments, and returns the response body as a byte array.

```go
response, err := client.Get("/api/1/users", nil)
```

- `Post`: This method makes a POST request to the specified path. It takes a `path`, a `queryParams` map, and a `body` interface{} as arguments, and returns the response body as a byte array.
### `newRequest` Function

```go
response, err := client.Post("/api/1/users", nil, user)
```
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.

- `Delete`: This method makes a DELETE request to the specified path. It takes a `path` and a `queryParams` map as arguments, and returns the response body as a byte array.
### `sendRequest` Function

```go
response, err := client.Delete("/api/1/users/1", nil)
```
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.

- `Put`: This method makes a PUT request to the specified path. It takes a `path`, a `queryParams` map, and a `body` interface{} as arguments, and returns the response body as a byte array.

```go
response, err := client.Put("/api/1/users/1", nil, updatedUser)
```

## Request

The `Request` struct represents an API request. It contains the `Url`, `Headers`, and `Body` of the request.

A new `Request` can be created using the `NewRequest` function, which takes a `url`, a `headers` map, and a `body` interface{} as arguments.
## HTTP Methods

The `Send` method of the `Request` struct sends the API request and returns an `http.Response`.
The `Client` struct provides the following methods for making HTTP requests:

```go
request := api.NewRequest("https://your-api-subdomain.onelogin.com/api/1/users", headers, body)
response, err := request.Send()
```
- `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.

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

The `Response` struct represents an API response. It contains the `StatusCode`, `Body`, and `Headers` of the response.
## Authenticator

A new `Response` can be created using the `NewResponse` function, which takes an `http.Response` as an argument.
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.

```go
response, err := api.NewResponse(httpResponse)
```
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.

Note: Make sure to check the `StatusCode` of the `Response` to ensure that the request was successful. A `StatusCode` in the 200-299 range indicates success, while a `StatusCode` in the 400-499 range indicates an error.
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.
29 changes: 10 additions & 19 deletions docs/authentication.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
# Authentication Module Documentation

The Authentication module provides functionalities to handle the authentication process with the OneLogin API. 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.
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` and `OLDomain` as fields:
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
OLDomain *string
accessToken string
}
```

## NewAuthenticator Function

The `NewAuthenticator` function is used to create a new `Authenticator` instance:
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 {
var domain = Domain
var token string = ""
return &Authenticator{
&token, &domain,
}
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 and also returned by the 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() (string, error) {
func (a *Authenticator) GenerateToken() error {
// implementation details
}
```

## RevokeToken Function

The `RevokeToken` function is used to revoke an existing access token. Similar to `GenerateToken`, it reads the `ONELOGIN_CLIENT_ID` and `ONELOGIN_CLIENT_SECRET` environment variables, creates a revocation request, sends it, and handles the response:
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 {
Expand All @@ -49,11 +44,11 @@ func (a *Authenticator) RevokeToken(token, domain *string) error {

## GetToken Function

The `GetToken` function is used to retrieve the current access token from the `Authenticator` instance:
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
return a.accessToken, nil
}
```

Expand All @@ -66,7 +61,3 @@ Errors in the Authentication module are represented using the `olError` package,
- `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.

## Conclusion

The Authentication module plays a crucial role in using the OneLogin Go SDK, as it handles the process of generating and revoking access tokens, which are necessary for making authenticated requests to the API. By understanding how this module works, you can handle authentication in your applications more effectively.
5 changes: 2 additions & 3 deletions internal/utilities/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -35,7 +34,7 @@ func CheckHTTPResponse(resp *http.Response) (interface{}, error) {
// Try to unmarshal the response body into a map[string]interface{} or []interface{}
var data interface{}
bodyStr := string(body)
log.Printf("Response body: %s\n", bodyStr)
//log.Printf("Response body: %s\n", bodyStr)
if strings.HasPrefix(bodyStr, "[") {
var slice []interface{}
err = json.Unmarshal(body, &slice)
Expand All @@ -54,7 +53,7 @@ func CheckHTTPResponse(resp *http.Response) (interface{}, error) {
data = bodyStr
}

log.Printf("Response body unmarshaled successfully: %v\n", data)
//log.Printf("Response body unmarshaled successfully: %v\n", data)
return data, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/onelogin/api_authorizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (sdk *OneloginSDK) CreateAuthServer(authServer *mod.AuthServer) (interface{
return utl.CheckHTTPResponse(resp)
}

// was ListAuthServers
func (sdk *OneloginSDK) GetAuthServers(queryParams mod.Queryable) (interface{}, error) {
p, err := utl.BuildAPIPath(APIAuthPath)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/onelogin/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (sdk *OneloginSDK) CreateApp(app *mod.App) (interface{}, error) {

}

// was ListApps
func (sdk *OneloginSDK) GetApps(queryParams mod.Queryable) (interface{}, error) {
p, err := utl.BuildAPIPath(AppPath)
if err != nil {
Expand Down
Loading

0 comments on commit 294258a

Please sign in to comment.