| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/url" |
| 6 | "strconv" |
| 7 | ) |
| 8 | |
| 9 | type AuditLogResponse struct { |
| 10 | Entries []AuditEntry `json:"entries"` |
| 11 | SlackResponse |
| 12 | } |
| 13 | |
| 14 | type AuditEntry struct { |
| 15 | ID string `json:"id"` |
| 16 | DateCreate int `json:"date_create"` |
| 17 | Action string `json:"action"` |
| 18 | Actor struct { |
| 19 | Type string `json:"type"` |
| 20 | User AuditUser `json:"user"` |
| 21 | } `json:"actor"` |
| 22 | Entity struct { |
| 23 | Type string `json:"type"` |
| 24 | // Only one of the below will be completed, based on the value of Type a user, a channel, a file, an app, a workspace, or an enterprise |
| 25 | User AuditUser `json:"user"` |
| 26 | Channel AuditChannel `json:"channel"` |
| 27 | File AuditFile `json:"file"` |
| 28 | App AuditApp `json:"app"` |
| 29 | Workspace AuditWorkspace `json:"workspace"` |
| 30 | Enterprise AuditEnterprise `json:"enterprise"` |
| 31 | } `json:"entity"` |
| 32 | Context struct { |
| 33 | Location struct { |
| 34 | Type string `json:"type"` |
| 35 | ID string `json:"id"` |
| 36 | Name string `json:"name"` |
| 37 | Domain string `json:"domain"` |
| 38 | } `json:"location"` |
| 39 | UA string `json:"ua"` |
| 40 | IPAddress string `json:"ip_address"` |
| 41 | } `json:"context"` |
| 42 | Details struct { |
| 43 | NewValue interface{} `json:"new_value"` |
| 44 | PreviousValue interface{} `json:"previous_value"` |
| 45 | MobileOnly bool `json:"mobile_only"` |
| 46 | WebOnly bool `json:"web_only"` |
| 47 | NonSSOOnly bool `json:"non_sso_only"` |
| 48 | ExportType string `json:"export_type"` |
| 49 | ExportStart string `json:"export_start_ts"` |
| 50 | ExportEnd string `json:"export_end_ts"` |
| 51 | } `json:"details"` |
| 52 | } |
| 53 | |
| 54 | type AuditUser struct { |
| 55 | ID string `json:"id"` |
| 56 | Name string `json:"name"` |
| 57 | Email string `json:"email"` |
| 58 | Team string `json:"team"` |
| 59 | } |
| 60 | |
| 61 | type AuditChannel struct { |
| 62 | ID string `json:"id"` |
| 63 | Name string `json:"name"` |
| 64 | Privacy string `json:"privacy"` |
| 65 | IsShared bool `json:"is_shared"` |
| 66 | IsOrgShared bool `json:"is_org_shared"` |
| 67 | } |
| 68 | |
| 69 | type AuditFile struct { |
| 70 | ID string `json:"id"` |
| 71 | Name string `json:"name"` |
| 72 | Filetype string `json:"filetype"` |
| 73 | Title string `json:"title"` |
| 74 | } |
| 75 | |
| 76 | type AuditApp struct { |
| 77 | ID string `json:"id"` |
| 78 | Name string `json:"name"` |
| 79 | IsDistributed bool `json:"is_distributed"` |
| 80 | IsDirectoryApproved bool `json:"is_directory_approved"` |
| 81 | IsWorkflowApp bool `json:"is_workflow_app"` |
| 82 | Scopes []string `json:"scopes"` |
| 83 | } |
| 84 | |
| 85 | type AuditWorkspace struct { |
| 86 | ID string `json:"id"` |
| 87 | Name string `json:"name"` |
| 88 | Domain string `json:"domain"` |
| 89 | } |
| 90 | |
| 91 | type AuditEnterprise struct { |
| 92 | ID string `json:"id"` |
| 93 | Name string `json:"name"` |
| 94 | Domain string `json:"domain"` |
| 95 | } |
| 96 | |
| 97 | // AuditLogParameters contains all the parameters necessary (including the optional ones) for a GetAuditLogs() request |
| 98 | type AuditLogParameters struct { |
| 99 | Limit int |
| 100 | Cursor string |
| 101 | Latest int |
| 102 | Oldest int |
| 103 | Action string |
| 104 | Actor string |
| 105 | Entity string |
| 106 | } |
| 107 | |
| 108 | func (api *Client) auditLogsRequest(ctx context.Context, path string, values url.Values) (*AuditLogResponse, error) { |
| 109 | response := &AuditLogResponse{} |
| 110 | err := api.getMethod(ctx, path, api.token, values, response) |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | return response, response.Err() |
| 115 | } |
| 116 | |
| 117 | // GetAuditLogs retrieves a page of audit entires according to the parameters given |
| 118 | func (api *Client) GetAuditLogs(params AuditLogParameters) (entries []AuditEntry, nextCursor string, err error) { |
| 119 | return api.GetAuditLogsContext(context.Background(), params) |
| 120 | } |
| 121 | |
| 122 | // GetAuditLogsContext retrieves a page of audit entries according to the parameters given with a custom context |
| 123 | func (api *Client) GetAuditLogsContext(ctx context.Context, params AuditLogParameters) (entries []AuditEntry, nextCursor string, err error) { |
| 124 | values := url.Values{} |
| 125 | if params.Limit != 0 { |
| 126 | values.Add("limit", strconv.Itoa(params.Limit)) |
| 127 | } |
| 128 | if params.Oldest != 0 { |
| 129 | values.Add("oldest", strconv.Itoa(params.Oldest)) |
| 130 | } |
| 131 | if params.Latest != 0 { |
| 132 | values.Add("latest", strconv.Itoa(params.Latest)) |
| 133 | } |
| 134 | if params.Cursor != "" { |
| 135 | values.Add("cursor", params.Cursor) |
| 136 | } |
| 137 | if params.Action != "" { |
| 138 | values.Add("action", params.Action) |
| 139 | } |
| 140 | if params.Actor != "" { |
| 141 | values.Add("actor", params.Actor) |
| 142 | } |
| 143 | if params.Entity != "" { |
| 144 | values.Add("entity", params.Entity) |
| 145 | } |
| 146 | |
| 147 | response, err := api.auditLogsRequest(ctx, "audit/v1/logs", values) |
| 148 | if err != nil { |
| 149 | return nil, "", err |
| 150 | } |
| 151 | return response.Entries, response.ResponseMetadata.Cursor, response.Err() |
| 152 | } |