-
Notifications
You must be signed in to change notification settings - Fork 0
[BB-766] User accounts provisioning and connector refactor #10
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1276d83
refactor: several modifications at the connector structure to normali…
JavierCarnelli-ConductorOne b04cf66
feat: user deprovisioning included
JavierCarnelli-ConductorOne cbb5312
refactor: client functions and pagination behavior refactored
JavierCarnelli-ConductorOne eb9b05d
feat: account creation is now supported
JavierCarnelli-ConductorOne 9473ad6
refactor: Users list function was refactored to use SCIM API. The nee…
JavierCarnelli-ConductorOne 890564d
docs: documentation updated
JavierCarnelli-ConductorOne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| ## Connector capabilities | ||
|
|
||
| 1. What resources does the connector sync? | ||
| - Notion connector syncs Users and Groups. | ||
|
|
||
| 2. Can the connector provision any resources? If so, which ones? | ||
| - This connector can provision Accounts. | ||
|
|
||
| ## Connector credentials | ||
|
|
||
| 1. What credentials or information are needed to set up the connector? (For example, API key, client ID and secret, domain, etc.) | ||
| - An API Token for the SCIM API should be provided. | ||
|
|
||
| 2. For each item in the list above: | ||
|
|
||
| * How does a user create or look up that credential or info? Please include links to (non-gated) documentation, screenshots (of the UI or of gated docs), or a video of the process. | ||
| - In order to generate an API Token for the SCIM API, a Organization Owner of an Enterprise Notion account should go to the settings panel. | ||
| - On the settings search for the 'Identity' tab on the left-panel and scroll down to the "SCIM Provisioning" section. | ||
| - In the "SCIM Provisioning" section, users should be able to create a Token to use the SCIM API. | ||
|
|
||
| * Does the credential need any specific scopes or permissions? If so, list them here. | ||
| note: this isn't specified on the Notion docs and we didn't have the chance to test it 'cause we don't have an enterprise instance. | ||
|
|
||
| * If applicable: Is the list of scopes or permissions different to sync (read) versus provision (read-write)? If so, list the difference here. | ||
| note: this isn't specified on the Notion docs and we didn't have the chance to test it 'cause we don't have an enterprise instance. | ||
|
|
||
| * What level of access or permissions does the user need in order to create the credentials? (For example, must be a super administrator, must have access to the admin console, etc.) | ||
| - It should be an Organization Owner. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
|
|
||
| "github.com/conductorone/baton-sdk/pkg/uhttp" | ||
| "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" | ||
| ) | ||
|
|
||
| const ( | ||
| baseUrl = "https://www.notion.so/scim/v2" | ||
| DefaultUserSchema = "urn:ietf:params:scim:schemas:core:2.0:User" | ||
| ) | ||
|
|
||
| type NotionClient struct { | ||
| client *uhttp.BaseHttpClient | ||
| scimToken string | ||
| } | ||
|
|
||
| func (c *NotionClient) GetUsers(ctx context.Context, pageOps PaginationOptions) ([]User, string, error) { | ||
| var nextPage string | ||
| requestURL := fmt.Sprint(baseUrl, "/Users") | ||
|
|
||
| var res UsersResponse | ||
| _, err := c.doRequest( | ||
| ctx, | ||
| http.MethodGet, | ||
| requestURL, | ||
| &res, | ||
| nil, | ||
| WithPageSize(pageOps.PerPage), | ||
| WithStartIndex(pageOps.StartIndex), | ||
| ) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
|
|
||
| if (int64(pageOps.StartIndex) + res.ItemsPerPage) < res.TotalResults { | ||
| nextPage = strconv.FormatInt(int64(pageOps.StartIndex)+res.ItemsPerPage, 10) | ||
| } | ||
|
|
||
| return res.Resources, nextPage, nil | ||
| } | ||
|
|
||
| // GetGroups returns all Notion groups. | ||
| func (c *NotionClient) GetGroups(ctx context.Context, pageOps PaginationOptions) ([]Group, string, error) { | ||
| var nextPage string | ||
| requestURL := fmt.Sprint(baseUrl, "/Groups") | ||
|
|
||
| var res GroupsResponse | ||
| _, err := c.doRequest( | ||
| ctx, | ||
| http.MethodGet, | ||
| requestURL, | ||
| &res, | ||
| nil, | ||
| WithPageSize(pageOps.PerPage), | ||
| WithStartIndex(pageOps.StartIndex), | ||
| ) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
|
|
||
| if (int64(pageOps.StartIndex) + res.ItemsPerPage) < res.TotalResults { | ||
| nextPage = strconv.FormatInt(int64(pageOps.StartIndex)+res.ItemsPerPage, 10) | ||
| } | ||
|
|
||
| return res.Resources, nextPage, nil | ||
| } | ||
|
|
||
| // GetGroup returns group details by group ID. | ||
| func (c *NotionClient) GetGroup(ctx context.Context, groupId string) (Group, error) { | ||
| requestURL := fmt.Sprint(baseUrl, "/Groups/", groupId) | ||
|
|
||
| var groupResponse Group | ||
| _, err := c.doRequest(ctx, http.MethodGet, requestURL, &groupResponse, nil) | ||
| if err != nil { | ||
| return Group{}, err | ||
JavierCarnelli-ConductorOne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return groupResponse, nil | ||
| } | ||
|
|
||
| func (c *NotionClient) GetUser(ctx context.Context, userID string) (*User, error) { | ||
| var userData *User | ||
| requestURL := fmt.Sprint(baseUrl, "/Users/", userID) | ||
|
|
||
| _, err := c.doRequest(ctx, http.MethodGet, requestURL, &userData, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return userData, nil | ||
| } | ||
|
|
||
| func (c *NotionClient) CreateUser(ctx context.Context, user *User) (*User, error) { | ||
| var newUser *User | ||
| requestURL := fmt.Sprint(baseUrl, "/Users") | ||
|
|
||
| _, err := c.doRequest(ctx, http.MethodPost, requestURL, &newUser, user) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return newUser, nil | ||
| } | ||
|
|
||
| func (c *NotionClient) DeleteUser(ctx context.Context, userID string) error { | ||
| requestURL := fmt.Sprint(baseUrl, "/Users/", userID) | ||
|
|
||
| _, err := c.doRequest(ctx, http.MethodDelete, requestURL, nil, nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *NotionClient) doRequest( | ||
| ctx context.Context, | ||
| method string, | ||
| endpointUrl string, | ||
| res interface{}, | ||
| body interface{}, | ||
| reqOpts ...ReqOpt, | ||
| ) (http.Header, error) { | ||
| var resp *http.Response | ||
|
|
||
| urlAddress, err := url.Parse(endpointUrl) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| for _, o := range reqOpts { | ||
| o(urlAddress) | ||
| } | ||
|
|
||
| opts := []uhttp.RequestOption{uhttp.WithBearerToken(c.scimToken)} | ||
| if body != nil { | ||
| opts = append(opts, uhttp.WithAcceptJSONHeader(), uhttp.WithContentTypeJSONHeader(), uhttp.WithJSONBody(body)) | ||
| } | ||
|
|
||
| req, err := c.client.NewRequest( | ||
| ctx, | ||
| method, | ||
| urlAddress, | ||
| opts..., | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| switch method { | ||
| case http.MethodGet, http.MethodPut, http.MethodPost, http.MethodPatch: | ||
| var doOptions []uhttp.DoOption | ||
| if res != nil { | ||
| doOptions = append(doOptions, uhttp.WithResponse(&res)) | ||
| } | ||
| resp, err = c.client.Do(req, doOptions...) | ||
| if resp != nil { | ||
| defer resp.Body.Close() | ||
| } | ||
|
|
||
| case http.MethodDelete: | ||
| resp, err = c.client.Do(req) | ||
| if resp != nil { | ||
| defer resp.Body.Close() | ||
| } | ||
| } | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return resp.Header, nil | ||
| } | ||
|
|
||
| func New(ctx context.Context, scimToken string) (*NotionClient, error) { | ||
| httpClient, err := uhttp.NewClient(ctx, uhttp.WithLogger(true, ctxzap.Extract(ctx))) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cli, err := uhttp.NewBaseHttpClientWithContext(ctx, httpClient) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| notionClient := NotionClient{ | ||
| client: cli, | ||
| scimToken: scimToken, | ||
| } | ||
|
|
||
| return ¬ionClient, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package client | ||
|
|
||
| type Group struct { | ||
| Schemas []string `json:"schemas"` | ||
| ID string `json:"id"` | ||
| DisplayName string `json:"displayName"` | ||
| Members []Member `json:"members"` | ||
| } | ||
|
|
||
| type GroupsResponse struct { | ||
| TotalResults int64 `json:"totalResults"` | ||
| Resources []Group `json:"Resources"` | ||
| StartIndex int64 `json:"startIndex"` | ||
| ItemsPerPage int64 `json:"itemsPerPage"` | ||
| } | ||
|
|
||
| type Member struct { | ||
| Value string `json:"value"` | ||
| Ref string `json:"$ref"` | ||
| Type string `json:"type"` | ||
| } | ||
|
|
||
| type UsersResponse struct { | ||
| TotalResults int64 `json:"totalResults"` | ||
| Resources []User `json:"Resources"` | ||
| StartIndex int64 `json:"startIndex"` | ||
| ItemsPerPage int64 `json:"itemsPerPage"` | ||
| } | ||
|
|
||
| type User struct { | ||
| ID string `json:"id"` | ||
| Schemas []string `json:"schemas"` | ||
| UserName string `json:"userName"` // Username corresponds to the email of the account. | ||
| Name struct { | ||
| GivenName string `json:"givenName"` | ||
| FamilyName string `json:"familyName"` | ||
| Formatted string `json:"formatted"` | ||
| } `json:"name"` | ||
| Emails []struct { | ||
| Primary bool `json:"primary"` | ||
| Value string `json:"value"` | ||
| Type string `json:"type"` | ||
| } `json:"emails"` | ||
| // Title string `json:"title"` | ||
| Active bool `json:"active"` | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.