|
| 1 | +package gointelowl |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +type Details struct { |
| 13 | + Username string `json:"username"` |
| 14 | + FirstName string `json:"first_name"` |
| 15 | + LastName string `json:"last_name"` |
| 16 | + FullName string `json:"full_name"` |
| 17 | + Email string `json:"email"` |
| 18 | +} |
| 19 | + |
| 20 | +type AccessDetails struct { |
| 21 | + TotalSubmissions int `json:"total_submissions"` |
| 22 | + MonthSubmissions int `json:"month_submissions"` |
| 23 | +} |
| 24 | + |
| 25 | +type User struct { |
| 26 | + User Details `json:"user"` |
| 27 | + Access AccessDetails `json:"access"` |
| 28 | +} |
| 29 | + |
| 30 | +type UserService struct { |
| 31 | + client *IntelOwlClient |
| 32 | +} |
| 33 | + |
| 34 | +type Owner struct { |
| 35 | + Username string `json:"username"` |
| 36 | + FullName string `json:"full_name"` |
| 37 | + Joined time.Time `json:"joined"` |
| 38 | +} |
| 39 | + |
| 40 | +type Organization struct { |
| 41 | + MembersCount int `json:"members_count"` |
| 42 | + Owner Owner `json:"owner"` |
| 43 | + IsUserOwner bool `json:"is_user_owner,omitempty"` |
| 44 | + CreatedAt *time.Time `json:"created_at,omitempty"` |
| 45 | + Name string `json:"name"` |
| 46 | +} |
| 47 | + |
| 48 | +type OrganizationParams struct { |
| 49 | + Name string `json:"name"` |
| 50 | +} |
| 51 | + |
| 52 | +type MemberParams struct { |
| 53 | + Username string `json:"username"` |
| 54 | +} |
| 55 | + |
| 56 | +type Invite struct { |
| 57 | + Id int `json:"id"` |
| 58 | + CreatedAt time.Time `json:"created_at"` |
| 59 | + Status string `json:"status"` |
| 60 | +} |
| 61 | + |
| 62 | +type Invitation struct { |
| 63 | + Invite |
| 64 | + Organization Organization `json:"organization"` |
| 65 | +} |
| 66 | + |
| 67 | +type InvitationParams struct { |
| 68 | + Organization OrganizationParams `json:"organization"` |
| 69 | + Status string `json:"status"` |
| 70 | +} |
| 71 | + |
| 72 | +// Access retrieves user details |
| 73 | +// |
| 74 | +// Endpoint: GET /api/me/access |
| 75 | +// |
| 76 | +// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_access_retrieve |
| 77 | +func (userService *UserService) Access(ctx context.Context) (*User, error) { |
| 78 | + requestUrl := fmt.Sprintf("%s/api/me/access", userService.client.options.Url) |
| 79 | + contentType := "application/json" |
| 80 | + method := "GET" |
| 81 | + request, err := userService.client.buildRequest(ctx, method, contentType, nil, requestUrl) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + user := User{} |
| 86 | + successResp, err := userService.client.newRequest(ctx, request) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + if unmarshalError := json.Unmarshal(successResp.Data, &user); unmarshalError != nil { |
| 91 | + return nil, unmarshalError |
| 92 | + } |
| 93 | + return &user, nil |
| 94 | +} |
| 95 | + |
| 96 | +// Organization returns the organization's details. |
| 97 | +// |
| 98 | +// Endpoint: GET /api/me/organization |
| 99 | +// |
| 100 | +// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_list |
| 101 | +func (userService *UserService) Organization(ctx context.Context) (*Organization, error) { |
| 102 | + requestUrl := fmt.Sprintf("%s/api/me/organization", userService.client.options.Url) |
| 103 | + contentType := "application/json" |
| 104 | + method := "GET" |
| 105 | + request, err := userService.client.buildRequest(ctx, method, contentType, nil, requestUrl) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + org := Organization{} |
| 111 | + successResp, err := userService.client.newRequest(ctx, request) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + if unmarshalError := json.Unmarshal(successResp.Data, &org); unmarshalError != nil { |
| 116 | + return nil, unmarshalError |
| 117 | + } |
| 118 | + return &org, nil |
| 119 | +} |
| 120 | + |
| 121 | +// CreateOrganization allows you to create a super cool organization! |
| 122 | +// |
| 123 | +// Endpoint: POST /api/me/organization |
| 124 | +// |
| 125 | +// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_create |
| 126 | +func (userService *UserService) CreateOrganization(ctx context.Context, organizationParams *OrganizationParams) (*Organization, error) { |
| 127 | + requestUrl := fmt.Sprintf("%s/api/me/organization", userService.client.options.Url) |
| 128 | + // Getting the relevant JSON data |
| 129 | + orgJson, err := json.Marshal(organizationParams) |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + contentType := "application/json" |
| 134 | + method := "POST" |
| 135 | + body := bytes.NewBuffer(orgJson) |
| 136 | + request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + org := Organization{} |
| 142 | + successResp, err := userService.client.newRequest(ctx, request) |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + if unmarshalError := json.Unmarshal(successResp.Data, &org); unmarshalError != nil { |
| 147 | + return nil, unmarshalError |
| 148 | + } |
| 149 | + return &org, nil |
| 150 | +} |
| 151 | + |
| 152 | +// InviteToOrganization allows you to invite someone to your super cool organization! |
| 153 | +// This is only accessible to the organization's owner. |
| 154 | +// |
| 155 | +// Endpoint: POST /api/me/organization/invite |
| 156 | +// |
| 157 | +// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_invite_create |
| 158 | +func (userService *UserService) InviteToOrganization(ctx context.Context, memberParams *MemberParams) (*Invite, error) { |
| 159 | + requestUrl := fmt.Sprintf("%s/api/me/organization/invite", userService.client.options.Url) |
| 160 | + // Getting the relevant JSON data |
| 161 | + memberJson, err := json.Marshal(memberParams) |
| 162 | + if err != nil { |
| 163 | + return nil, err |
| 164 | + } |
| 165 | + contentType := "application/json" |
| 166 | + method := "POST" |
| 167 | + body := bytes.NewBuffer(memberJson) |
| 168 | + request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl) |
| 169 | + if err != nil { |
| 170 | + return nil, err |
| 171 | + } |
| 172 | + |
| 173 | + invite := Invite{} |
| 174 | + successResp, err := userService.client.newRequest(ctx, request) |
| 175 | + if err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + if unmarshalError := json.Unmarshal(successResp.Data, &invite); unmarshalError != nil { |
| 179 | + return nil, unmarshalError |
| 180 | + } |
| 181 | + return &invite, nil |
| 182 | +} |
| 183 | + |
| 184 | +// RemoveMemberFromOrganization lets you remove someone from your super cool organization! (you had your reasons) |
| 185 | +// This is only accessible to the organization's owner. |
| 186 | +// |
| 187 | +// Endpoint: POST /api/me/organization/remove_member |
| 188 | +// |
| 189 | +// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_create |
| 190 | +func (userService *UserService) RemoveMemberFromOrganization(ctx context.Context, memberParams *MemberParams) (bool, error) { |
| 191 | + requestUrl := fmt.Sprintf("%s/api/me/organization/remove_member", userService.client.options.Url) |
| 192 | + // Getting the relevant JSON data |
| 193 | + memberJson, err := json.Marshal(memberParams) |
| 194 | + if err != nil { |
| 195 | + return false, err |
| 196 | + } |
| 197 | + contentType := "application/json" |
| 198 | + method := "POST" |
| 199 | + body := bytes.NewBuffer(memberJson) |
| 200 | + request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl) |
| 201 | + if err != nil { |
| 202 | + return false, err |
| 203 | + } |
| 204 | + |
| 205 | + successResp, err := userService.client.newRequest(ctx, request) |
| 206 | + if err != nil { |
| 207 | + return false, err |
| 208 | + } |
| 209 | + |
| 210 | + if successResp.StatusCode == http.StatusNoContent { |
| 211 | + return true, nil |
| 212 | + } |
| 213 | + return false, nil |
| 214 | +} |
0 commit comments