This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support notifications to users with private email ( using fabric8 ser…
…vice account ) (#54) * start using latest auth * get SA token * inject service account token on startup * remove the WITs user api call * add custom code for user api client
- Loading branch information
Showing
20 changed files
with
403 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"fmt" | ||
|
||
"github.com/fabric8-services/fabric8-notification/auth/api" | ||
"github.com/fabric8-services/fabric8-wit/goasupport" | ||
goaclient "github.com/goadesign/goa/client" | ||
"github.com/goadesign/goa/uuid" | ||
"github.com/gregjones/httpcache" | ||
) | ||
|
||
func NewCachedClient(hostURL string) (*api.Client, error) { | ||
|
||
u, err := url.Parse(hostURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tp := httpcache.NewMemoryCacheTransport() | ||
client := http.Client{Transport: tp} | ||
|
||
c := api.New(goaclient.HTTPClientDoer(&client)) | ||
c.Host = u.Host | ||
c.Scheme = u.Scheme | ||
return c, nil | ||
} | ||
|
||
func GetSpaceCollaborators(ctx context.Context, client *api.Client, spaceID uuid.UUID) (*api.UserList, error) { | ||
pageLimit := 100 | ||
pageOffset := "0" | ||
resp, err := client.ListCollaborators(goasupport.ForwardContextRequestID(ctx), api.ListCollaboratorsPath(spaceID), &pageLimit, &pageOffset, nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp != nil { | ||
defer resp.Body.Close() | ||
} else { | ||
return nil, fmt.Errorf("failed to make request to get list of collaborators") | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("non %v status code for %v, returned %v", http.StatusOK, "GET collaborators", resp.StatusCode) | ||
} | ||
return client.DecodeUserList(resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package auth_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/fabric8-services/fabric8-notification/auth" | ||
"github.com/fabric8-services/fabric8-notification/auth/api" | ||
"github.com/goadesign/goa/uuid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
openshiftIOAPI = "http://auth.openshift.io" | ||
) | ||
|
||
func createClient(t *testing.T) *api.Client { | ||
c, err := auth.NewCachedClient(openshiftIOAPI) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return c | ||
} | ||
|
||
func TestSpaceCollaborators(t *testing.T) { | ||
|
||
c := createClient(t) | ||
id, _ := uuid.FromString("020f756e-b51a-4b43-b113-45cec16b9ce9") | ||
|
||
u, err := auth.GetSpaceCollaborators(context.Background(), c, id) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.True(t, len(u.Data) > 10) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"fmt" | ||
|
||
"github.com/fabric8-services/fabric8-notification/auth/api" | ||
"github.com/fabric8-services/fabric8-wit/goasupport" | ||
"github.com/goadesign/goa/uuid" | ||
) | ||
|
||
/* | ||
Took out the auth api client code relevant to User API in order to add a custom | ||
client.JWTSigner.Sign(req) before the http request is made. | ||
This wasn't present in the auto-generated client for GET /api/users/ID | ||
because we don't set "a.Security("jwt")" in auth's design/account.go | ||
for the `Show Users` action. | ||
*/ | ||
|
||
func GetUser(ctx context.Context, client *api.Client, uID uuid.UUID) (*api.User, error) { | ||
resp, err := showUsers(goasupport.ForwardContextRequestID(ctx), client, api.ShowUsersPath(uID.String()), nil, nil) | ||
if resp != nil { | ||
defer resp.Body.Close() | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("non %v status code for %v, returned %v", http.StatusOK, "GET user", resp.StatusCode) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return client.DecodeUser(resp) | ||
} | ||
|
||
// retrieve user for the given ID. | ||
func showUsers(ctx context.Context, client *api.Client, path string, ifModifiedSince *string, ifNoneMatch *string) (*http.Response, error) { | ||
req, err := newShowUsersRequest(ctx, client, path, ifModifiedSince, ifNoneMatch) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return client.Do(ctx, req) | ||
} | ||
|
||
// newShowUsersRequest create the request corresponding to the show action endpoint of the users resource. | ||
func newShowUsersRequest(ctx context.Context, client *api.Client, path string, ifModifiedSince *string, ifNoneMatch *string) (*http.Request, error) { | ||
scheme := client.Scheme | ||
if scheme == "" { | ||
scheme = "http" | ||
} | ||
u := url.URL{Host: client.Host, Scheme: scheme, Path: path} | ||
req, err := http.NewRequest("GET", u.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
header := req.Header | ||
if ifModifiedSince != nil { | ||
|
||
header.Set("If-Modified-Since", *ifModifiedSince) | ||
} | ||
if ifNoneMatch != nil { | ||
|
||
header.Set("If-None-Match", *ifNoneMatch) | ||
} | ||
|
||
// This wasn't present in the auto-generated client for GET /api/users/ID | ||
// because we don't set "a.Security("jwt")" in auth's design/account.go | ||
// for the `Show Users` action. | ||
if client.JWTSigner != nil { | ||
client.JWTSigner.Sign(req) | ||
} | ||
return req, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.