github.com/google/go-github/v68@v68.0.0/github/scim.go (about)

     1  // Copyright 2021 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  )
    13  
    14  // SCIMService provides access to SCIM related functions in the
    15  // GitHub API.
    16  //
    17  // GitHub API docs: https://docs.github.com/rest/scim
    18  type SCIMService service
    19  
    20  // SCIMUserAttributes represents supported SCIM User attributes.
    21  //
    22  // GitHub API docs: https://docs.github.com/rest/scim#supported-scim-user-attributes
    23  type SCIMUserAttributes struct {
    24  	UserName    string           `json:"userName"`              // Configured by the admin. Could be an email, login, or username. (Required.)
    25  	Name        SCIMUserName     `json:"name"`                  // (Required.)
    26  	DisplayName *string          `json:"displayName,omitempty"` // The name of the user, suitable for display to end-users. (Optional.)
    27  	Emails      []*SCIMUserEmail `json:"emails"`                // User emails. (Required.)
    28  	Schemas     []string         `json:"schemas,omitempty"`     // (Optional.)
    29  	ExternalID  *string          `json:"externalId,omitempty"`  // (Optional.)
    30  	Groups      []string         `json:"groups,omitempty"`      // (Optional.)
    31  	Active      *bool            `json:"active,omitempty"`      // (Optional.)
    32  	// Only populated as a result of calling ListSCIMProvisionedIdentitiesOptions or GetSCIMProvisioningInfoForUser:
    33  	ID   *string   `json:"id,omitempty"`
    34  	Meta *SCIMMeta `json:"meta,omitempty"`
    35  }
    36  
    37  // SCIMUserName represents SCIM user information.
    38  type SCIMUserName struct {
    39  	GivenName  string  `json:"givenName"`           // The first name of the user. (Required.)
    40  	FamilyName string  `json:"familyName"`          // The family name of the user. (Required.)
    41  	Formatted  *string `json:"formatted,omitempty"` // (Optional.)
    42  }
    43  
    44  // SCIMUserEmail represents SCIM user email.
    45  type SCIMUserEmail struct {
    46  	Value   string  `json:"value"`             // (Required.)
    47  	Primary *bool   `json:"primary,omitempty"` // (Optional.)
    48  	Type    *string `json:"type,omitempty"`    // (Optional.)
    49  }
    50  
    51  // SCIMMeta represents metadata about the SCIM resource.
    52  type SCIMMeta struct {
    53  	ResourceType *string    `json:"resourceType,omitempty"`
    54  	Created      *Timestamp `json:"created,omitempty"`
    55  	LastModified *Timestamp `json:"lastModified,omitempty"`
    56  	Location     *string    `json:"location,omitempty"`
    57  }
    58  
    59  // SCIMProvisionedIdentities represents the result of calling ListSCIMProvisionedIdentities.
    60  type SCIMProvisionedIdentities struct {
    61  	Schemas      []string              `json:"schemas,omitempty"`
    62  	TotalResults *int                  `json:"totalResults,omitempty"`
    63  	ItemsPerPage *int                  `json:"itemsPerPage,omitempty"`
    64  	StartIndex   *int                  `json:"startIndex,omitempty"`
    65  	Resources    []*SCIMUserAttributes `json:"Resources,omitempty"`
    66  }
    67  
    68  // ListSCIMProvisionedIdentitiesOptions represents options for ListSCIMProvisionedIdentities.
    69  //
    70  // GitHub API docs: https://docs.github.com/rest/scim#list-scim-provisioned-identities--parameters
    71  type ListSCIMProvisionedIdentitiesOptions struct {
    72  	StartIndex *int `url:"startIndex,omitempty"` // Used for pagination: the index of the first result to return. (Optional.)
    73  	Count      *int `url:"count,omitempty"`      // Used for pagination: the number of results to return. (Optional.)
    74  	// Filter results using the equals query parameter operator (eq).
    75  	// You can filter results that are equal to id, userName, emails, and external_id.
    76  	// For example, to search for an identity with the userName Octocat, you would use this query: ?filter=userName%20eq%20\"Octocat\".
    77  	// To filter results for the identity with the email octocat@github.com, you would use this query: ?filter=emails%20eq%20\"octocat@github.com\".
    78  	// (Optional.)
    79  	Filter *string `url:"filter,omitempty"`
    80  }
    81  
    82  // ListSCIMProvisionedIdentities lists SCIM provisioned identities.
    83  //
    84  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/scim/scim#list-scim-provisioned-identities
    85  //
    86  //meta:operation GET /scim/v2/organizations/{org}/Users
    87  func (s *SCIMService) ListSCIMProvisionedIdentities(ctx context.Context, org string, opts *ListSCIMProvisionedIdentitiesOptions) (*SCIMProvisionedIdentities, *Response, error) {
    88  	u := fmt.Sprintf("scim/v2/organizations/%v/Users", org)
    89  	u, err := addOptions(u, opts)
    90  	if err != nil {
    91  		return nil, nil, err
    92  	}
    93  
    94  	req, err := s.client.NewRequest("GET", u, nil)
    95  	if err != nil {
    96  		return nil, nil, err
    97  	}
    98  
    99  	identities := new(SCIMProvisionedIdentities)
   100  	resp, err := s.client.Do(ctx, req, identities)
   101  	if err != nil {
   102  		return nil, resp, err
   103  	}
   104  
   105  	return identities, resp, nil
   106  }
   107  
   108  // ProvisionAndInviteSCIMUser provisions organization membership for a user, and sends an activation email to the email address.
   109  //
   110  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/scim/scim#provision-and-invite-a-scim-user
   111  //
   112  //meta:operation POST /scim/v2/organizations/{org}/Users
   113  func (s *SCIMService) ProvisionAndInviteSCIMUser(ctx context.Context, org string, opts *SCIMUserAttributes) (*SCIMUserAttributes, *Response, error) {
   114  	u := fmt.Sprintf("scim/v2/organizations/%v/Users", org)
   115  
   116  	req, err := s.client.NewRequest("POST", u, opts)
   117  	if err != nil {
   118  		return nil, nil, err
   119  	}
   120  
   121  	user := new(SCIMUserAttributes)
   122  	resp, err := s.client.Do(ctx, req, user)
   123  	if err != nil {
   124  		return nil, resp, err
   125  	}
   126  
   127  	return user, resp, nil
   128  }
   129  
   130  // GetSCIMProvisioningInfoForUser returns SCIM provisioning information for a user.
   131  //
   132  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/scim/scim#get-scim-provisioning-information-for-a-user
   133  //
   134  //meta:operation GET /scim/v2/organizations/{org}/Users/{scim_user_id}
   135  func (s *SCIMService) GetSCIMProvisioningInfoForUser(ctx context.Context, org, scimUserID string) (*SCIMUserAttributes, *Response, error) {
   136  	u := fmt.Sprintf("scim/v2/organizations/%v/Users/%v", org, scimUserID)
   137  	req, err := s.client.NewRequest("GET", u, nil)
   138  	if err != nil {
   139  		return nil, nil, err
   140  	}
   141  
   142  	user := new(SCIMUserAttributes)
   143  	resp, err := s.client.Do(ctx, req, &user)
   144  	if err != nil {
   145  		return nil, resp, err
   146  	}
   147  
   148  	return user, resp, nil
   149  }
   150  
   151  // UpdateProvisionedOrgMembership updates a provisioned organization membership.
   152  //
   153  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/scim/scim#update-a-provisioned-organization-membership
   154  //
   155  //meta:operation PUT /scim/v2/organizations/{org}/Users/{scim_user_id}
   156  func (s *SCIMService) UpdateProvisionedOrgMembership(ctx context.Context, org, scimUserID string, opts *SCIMUserAttributes) (*Response, error) {
   157  	u := fmt.Sprintf("scim/v2/organizations/%v/Users/%v", org, scimUserID)
   158  	u, err := addOptions(u, opts)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	req, err := s.client.NewRequest("PUT", u, nil)
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  
   168  	return s.client.Do(ctx, req, nil)
   169  }
   170  
   171  // UpdateAttributeForSCIMUserOptions represents options for UpdateAttributeForSCIMUser.
   172  //
   173  // GitHub API docs: https://docs.github.com/rest/scim#update-an-attribute-for-a-scim-user--parameters
   174  type UpdateAttributeForSCIMUserOptions struct {
   175  	Schemas    []string                             `json:"schemas,omitempty"` // (Optional.)
   176  	Operations UpdateAttributeForSCIMUserOperations `json:"operations"`        // Set of operations to be performed. (Required.)
   177  }
   178  
   179  // UpdateAttributeForSCIMUserOperations represents operations for UpdateAttributeForSCIMUser.
   180  type UpdateAttributeForSCIMUserOperations struct {
   181  	Op    string          `json:"op"`              // (Required.)
   182  	Path  *string         `json:"path,omitempty"`  // (Optional.)
   183  	Value json.RawMessage `json:"value,omitempty"` // (Optional.)
   184  }
   185  
   186  // UpdateAttributeForSCIMUser updates an attribute for an SCIM user.
   187  //
   188  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/scim/scim#update-an-attribute-for-a-scim-user
   189  //
   190  //meta:operation PATCH /scim/v2/organizations/{org}/Users/{scim_user_id}
   191  func (s *SCIMService) UpdateAttributeForSCIMUser(ctx context.Context, org, scimUserID string, opts *UpdateAttributeForSCIMUserOptions) (*Response, error) {
   192  	u := fmt.Sprintf("scim/v2/organizations/%v/Users/%v", org, scimUserID)
   193  	u, err := addOptions(u, opts)
   194  	if err != nil {
   195  		return nil, err
   196  	}
   197  
   198  	req, err := s.client.NewRequest("PATCH", u, nil)
   199  	if err != nil {
   200  		return nil, err
   201  	}
   202  
   203  	return s.client.Do(ctx, req, nil)
   204  }
   205  
   206  // DeleteSCIMUserFromOrg deletes SCIM user from an organization.
   207  //
   208  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/scim/scim#delete-a-scim-user-from-an-organization
   209  //
   210  //meta:operation DELETE /scim/v2/organizations/{org}/Users/{scim_user_id}
   211  func (s *SCIMService) DeleteSCIMUserFromOrg(ctx context.Context, org, scimUserID string) (*Response, error) {
   212  	u := fmt.Sprintf("scim/v2/organizations/%v/Users/%v", org, scimUserID)
   213  	req, err := s.client.NewRequest("DELETE", u, nil)
   214  	if err != nil {
   215  		return nil, err
   216  	}
   217  
   218  	return s.client.Do(ctx, req, nil)
   219  }