github.com/google/go-github/v66@v66.0.0/github/orgs_credential_authorizations.go (about)

     1  // Copyright 2023 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  	"fmt"
    11  	"net/http"
    12  )
    13  
    14  // CredentialAuthorization represents a credential authorized through SAML SSO.
    15  type CredentialAuthorization struct {
    16  	// User login that owns the underlying credential.
    17  	Login *string `json:"login,omitempty"`
    18  
    19  	// Unique identifier for the credential.
    20  	CredentialID *int64 `json:"credential_id,omitempty"`
    21  
    22  	// Human-readable description of the credential type.
    23  	CredentialType *string `json:"credential_type,omitempty"`
    24  
    25  	// Last eight characters of the credential.
    26  	// Only included in responses with credential_type of personal access token.
    27  	TokenLastEight *string `json:"token_last_eight,omitempty"`
    28  
    29  	// Date when the credential was authorized for use.
    30  	CredentialAuthorizedAt *Timestamp `json:"credential_authorized_at,omitempty"`
    31  
    32  	// Date when the credential was last accessed.
    33  	// May be null if it was never accessed.
    34  	CredentialAccessedAt *Timestamp `json:"credential_accessed_at,omitempty"`
    35  
    36  	// List of oauth scopes the token has been granted.
    37  	Scopes []string `json:"scopes,omitempty"`
    38  
    39  	// Unique string to distinguish the credential.
    40  	// Only included in responses with credential_type of SSH Key.
    41  	Fingerprint *string `json:"fingerprint,omitempty"`
    42  
    43  	AuthorizedCredentialID *int64 `json:"authorized_credential_id,omitempty"`
    44  
    45  	// The title given to the ssh key.
    46  	// This will only be present when the credential is an ssh key.
    47  	AuthorizedCredentialTitle *string `json:"authorized_credential_title,omitempty"`
    48  
    49  	// The note given to the token.
    50  	// This will only be present when the credential is a token.
    51  	AuthorizedCredentialNote *string `json:"authorized_credential_note,omitempty"`
    52  
    53  	// The expiry for the token.
    54  	// This will only be present when the credential is a token.
    55  	AuthorizedCredentialExpiresAt *Timestamp `json:"authorized_credential_expires_at,omitempty"`
    56  }
    57  
    58  // CredentialAuthorizationsListOptions adds the Login option as supported by the
    59  // list SAML SSO authorizations for organizations endpoint alongside paging options
    60  // such as Page and PerPage.
    61  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/orgs#list-saml-sso-authorizations-for-an-organization
    62  type CredentialAuthorizationsListOptions struct {
    63  	ListOptions
    64  	// For credentials authorizations for an organization, limit the list of authorizations to a specific login (aka github username)
    65  	Login string `url:"login,omitempty"`
    66  }
    67  
    68  // ListCredentialAuthorizations lists credentials authorized through SAML SSO
    69  // for a given organization. Only available with GitHub Enterprise Cloud.
    70  //
    71  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/orgs#list-saml-sso-authorizations-for-an-organization
    72  //
    73  //meta:operation GET /orgs/{org}/credential-authorizations
    74  func (s *OrganizationsService) ListCredentialAuthorizations(ctx context.Context, org string, opts *CredentialAuthorizationsListOptions) ([]*CredentialAuthorization, *Response, error) {
    75  	u := fmt.Sprintf("orgs/%v/credential-authorizations", org)
    76  	u, err := addOptions(u, opts)
    77  	if err != nil {
    78  		return nil, nil, err
    79  	}
    80  
    81  	req, err := s.client.NewRequest(http.MethodGet, u, nil)
    82  	if err != nil {
    83  		return nil, nil, err
    84  	}
    85  
    86  	var creds []*CredentialAuthorization
    87  	resp, err := s.client.Do(ctx, req, &creds)
    88  	if err != nil {
    89  		return nil, resp, err
    90  	}
    91  
    92  	return creds, resp, nil
    93  }
    94  
    95  // RemoveCredentialAuthorization revokes the SAML SSO authorization for a given
    96  // credential within an organization. Only available with GitHub Enterprise Cloud.
    97  //
    98  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/orgs#remove-a-saml-sso-authorization-for-an-organization
    99  //
   100  //meta:operation DELETE /orgs/{org}/credential-authorizations/{credential_id}
   101  func (s *OrganizationsService) RemoveCredentialAuthorization(ctx context.Context, org string, credentialID int64) (*Response, error) {
   102  	u := fmt.Sprintf("orgs/%v/credential-authorizations/%v", org, credentialID)
   103  	req, err := s.client.NewRequest(http.MethodDelete, u, nil)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	return s.client.Do(ctx, req, nil)
   109  }