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 36b18b8
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions pkg/restapi/operation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/coreos/go-oidc"
"github.com/google/uuid"
"github.com/trustbloc/edge-core/pkg/log"
"github.com/trustbloc/edge-core/pkg/storage"
"golang.org/x/oauth2"
"net/http"
"net/url"

"github.com/trustbloc/hub-auth/pkg/bootstrap/user"
"github.com/trustbloc/hub-auth/pkg/internal/common/support"
Expand All @@ -27,6 +27,7 @@ import (
const (
oauth2GetRequestPath = "/oauth2/request"
oauth2CallbackPath = "/oauth2/callback"

// api path params
scopeQueryParam = "scope"

Expand Down Expand Up @@ -115,6 +116,7 @@ type Operation struct {
oidcClientID string
oidcClientSecret string
oidcCallbackURL string
uiEndpoint string
oauth2ConfigFunc func(...string) oauth2Config
bootstrapStore storage.Store
}
Expand All @@ -127,6 +129,7 @@ type Config struct {
OIDCClientID string
OIDCClientSecret string
OIDCCallbackURL string
UIEndpoint string
TransientStoreProvider storage.Provider
StoreProvider storage.Provider
}
Expand All @@ -135,14 +138,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 +327,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 +352,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

0 comments on commit 36b18b8

Please sign in to comment.