Skip to content

Commit

Permalink
feat: setup UI endpoint to serve the UI
Browse files Browse the repository at this point in the history
closes trustbloc#39

Signed-off-by: talwinder.kaur <talwinder.kaur@securekey.com>
  • Loading branch information
talwinder50 committed Jul 29, 2020
1 parent 39dba44 commit c0bd95d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
37 changes: 31 additions & 6 deletions pkg/restapi/operation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"encoding/json"
"errors"
"fmt"

"net/http"
"net/url"

"github.com/coreos/go-oidc"
oidc "github.com/coreos/go-oidc"
"github.com/google/uuid"
"github.com/trustbloc/edge-core/pkg/log"
"github.com/trustbloc/edge-core/pkg/storage"
Expand Down Expand Up @@ -115,6 +117,7 @@ type Operation struct {
oidcClientID string
oidcClientSecret string
oidcCallbackURL string
uiEndpoint string
oauth2ConfigFunc func(...string) oauth2Config
bootstrapStore storage.Store
}
Expand All @@ -127,6 +130,7 @@ type Config struct {
OIDCClientID string
OIDCClientSecret string
OIDCCallbackURL string
UIEndpoint string
TransientStoreProvider storage.Provider
StoreProvider storage.Provider
}
Expand All @@ -135,14 +139,15 @@ type createOIDCRequestResponse struct {
Request string `json:"request"`
}

// New returns rp operation instance.
// New returns hub-auth operation instance.
func New(config *Config) (*Operation, error) {
svc := &Operation{
client: &http.Client{Transport: &http.Transport{TLSClientConfig: config.TLSConfig}},
requestTokens: config.RequestTokens,
oidcClientID: config.OIDCClientID,
oidcClientSecret: config.OIDCClientSecret,
oidcCallbackURL: config.OIDCCallbackURL,
uiEndpoint: config.UIEndpoint,
}

// TODO implement retries: https://github.com/trustbloc/hub-auth/issues/45
Expand Down Expand Up @@ -323,7 +328,15 @@ func (c *Operation) handleOIDCCallback(w http.ResponseWriter, r *http.Request) {
return
}

handleAuthResult(w, r, userProfile)
profileBytes, err := json.Marshal(userProfile)
if err != nil {
c.writeErrorResponse(w, http.StatusInternalServerError,
fmt.Sprintf("failed to marshal user profile data : %s", err))

return
}

c.handleAuthResult(w, r, profileBytes)
}

// TODO onboard user at key server and SDS: https://github.com/trustbloc/hub-auth/issues/38
Expand All @@ -340,9 +353,21 @@ func (c *Operation) onboardUser(id string) (*user.Profile, error) {
return userProfile, nil
}

// TODO redirect to the UI: https://github.com/trustbloc/hub-auth/issues/39
func handleAuthResult(w http.ResponseWriter, r *http.Request, _ *user.Profile) {
http.Redirect(w, r, "", http.StatusFound)
func (c *Operation) handleAuthResult(w http.ResponseWriter, r *http.Request, profileBytes []byte) {
handle := url.QueryEscape(uuid.New().String())

err := c.transientStore.Put(handle, profileBytes)
if err != nil {
c.writeErrorResponse(w,
http.StatusInternalServerError, fmt.Sprintf("failed to write handle to transient store : %s", err))

return
}

redirectURL := fmt.Sprintf("%s?up=%s", c.uiEndpoint, handle)

http.Redirect(w, r, redirectURL, http.StatusFound)
logger.Debugf("redirected to: %s", redirectURL)
}

func handleAuthError(w http.ResponseWriter, status int, msg string) {
Expand Down
24 changes: 24 additions & 0 deletions pkg/restapi/operation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,30 @@ func TestHandleOIDCCallback(t *testing.T) {
svc.handleOIDCCallback(result, newOIDCCallback(state, "code"))
require.Equal(t, http.StatusInternalServerError, result.Code)
})
t.Run("PUT error while storing user info while handling callback user", func(t *testing.T) {
id := uuid.New().String()
state := uuid.New().String()
config := config(t)

config.TransientStoreProvider = &mockstorage.Provider{
Stores: map[string]storage.Store{
transientStoreName: &mockstore.MockStore{
Store: map[string][]byte{
id: []byte("{}"),
},
ErrGet: storage.ErrValueNotFound,
ErrPut: errors.New("generic"),
},
},
}

svc, err := New(config)
require.NoError(t, err)

result := httptest.NewRecorder()
svc.handleAuthResult(result, newOIDCCallback(state, "code"), nil)
require.Equal(t, http.StatusInternalServerError, result.Code)
})
}

func newCreateOIDCHTTPRequest(scope string) *http.Request {
Expand Down

0 comments on commit c0bd95d

Please sign in to comment.