Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ _testmain.go
*.test
*.prof
cmd

# Emacs
*~
\#*
.#*
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/rnikoopour/onelogin

require (
github.com/google/go-querystring v1.0.0
golang.org/x/crypto v0.0.0-20191028145041-f83a4685e152 // indirect
golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20191028164358-195ce5e7f934 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20191028173913-b394bd8bba1d // indirect
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect
)

go 1.13
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191028145041-f83a4685e152/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20181005035420-146acd28ed58 h1:otZG8yDCO4LVps5+9bxOeNiCvgmOyt96J3roHTYs7oE=
golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271 h1:N66aaryRB3Ax92gH0v3hp1QYZ3zWWCCUR/j8Ifh45Ss=
golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191028164358-195ce5e7f934/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191028173913-b394bd8bba1d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
79 changes: 67 additions & 12 deletions group.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
package onelogin

import "golang.org/x/net/context"
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"

"golang.org/x/net/context"
)

// GroupService deals with OneLogin groups.
type GroupService service

type Group struct {
ID int64 `json:"id"`
Name string `json:"name"`
ID int64 `json:"id"`
Name string `json:"name"`
Reference string `json:"reference"`
}

type GroupResponse struct {
Status struct {
Error bool `json:"error"`
Code int `json:"code"`
Type string `json:"type"`
Message string `json:"message`
} `json:"status"`
Pagination struct {
Before string `json:"before_cursor"`
After string `json:"after_curson"`
Previous string `json:"previous_link"`
Next string `json:"next_link"`
} `json:"pagination"`
Data []Group `json:"data"`
}

// GetGroups returns all the OneLogin groups.
func (s *GroupService) GetGroups(ctx context.Context) ([]*Group, error) {
func (s *GroupService) GetGroups(ctx context.Context) ([]Group, error) {
u := "/api/1/groups"

var groups []*Group
var afterCursor string
groups := []Group{}
var afterCursor string = ""

for {
uu, err := addOptions(u, &urlQuery{AfterCursor: afterCursor})
Expand All @@ -32,18 +56,49 @@ func (s *GroupService) GetGroups(ctx context.Context) ([]*Group, error) {
return nil, err
}

var gs []*Group
resp, err := s.client.Do(ctx, req, &gs)
resp, err := s.client.DoGroups(ctx, req)
if err != nil {
return nil, err
}
groups = append(groups, gs...)
if resp.PaginationAfterCursor == nil {

groups = append(groups, resp.Data...)

if resp.Pagination.After != "" {
afterCursor = resp.Pagination.After
} else {
break
}

afterCursor = *resp.PaginationAfterCursor
}

return groups, nil
}

func (c *Client) DoGroups(ctx context.Context, req *http.Request) (*GroupResponse, error) {
req = req.WithContext(ctx)

resp, err := c.client.Do(req)
if err != nil {
return nil, err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var groupResp *GroupResponse = nil
err = json.Unmarshal(body, groupResp)
if err != nil {
return nil, err
}

if groupResp == nil {
return nil, errors.New("empy group response from onelogin")
}

if groupResp.Status.Error {
return groupResp, errors.New("group response from onelogin returned error")
}

return groupResp, nil
}
Loading