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 <[email protected]>
  • Loading branch information
talwinder50 committed Jul 27, 2020
1 parent bb6ec17 commit a93ef61
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions pkg/restapi/operation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"sync"

"github.com/coreos/go-oidc"
"github.com/google/uuid"
Expand All @@ -27,6 +29,7 @@ import (
const (
oauth2GetRequestPath = "/oauth2/request"
oauth2CallbackPath = "/oauth2/callback"

// api path params
scopeQueryParam = "scope"

Expand Down Expand Up @@ -108,15 +111,18 @@ type oauth2Token interface {

// Operation defines handlers.
type Operation struct {
client httpClient
requestTokens map[string]string
transientStore storage.Store
oidcProvider oidcProvider
oidcClientID string
oidcClientSecret string
oidcCallbackURL string
oauth2ConfigFunc func(...string) oauth2Config
bootstrapStore storage.Store
client httpClient
requestTokens map[string]string
transientStore storage.Store
oidcProvider oidcProvider
oidcClientID string
oidcClientSecret string
oidcCallbackURL string
uiEndpoint string
userProfileRequest map[string]*user.Profile
presDefsLock sync.Mutex
oauth2ConfigFunc func(...string) oauth2Config
bootstrapStore storage.Store
}

// Config defines configuration for rp operations.
Expand All @@ -127,6 +133,7 @@ type Config struct {
OIDCClientID string
OIDCClientSecret string
OIDCCallbackURL string
UIEndpoint string
TransientStoreProvider storage.Provider
StoreProvider storage.Provider
}
Expand All @@ -135,14 +142,16 @@ 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,
client: &http.Client{Transport: &http.Transport{TLSClientConfig: config.TLSConfig}},
requestTokens: config.RequestTokens,
oidcClientID: config.OIDCClientID,
oidcClientSecret: config.OIDCClientSecret,
oidcCallbackURL: config.OIDCCallbackURL,
userProfileRequest: make(map[string]*user.Profile),
uiEndpoint: config.UIEndpoint,
}
// TODO remove this empty string check. This is just here temporarily to allow for testing of other parts
// while the OIDC stuff is in development
Expand Down Expand Up @@ -325,7 +334,7 @@ func (c *Operation) handleOIDCCallback(w http.ResponseWriter, r *http.Request) {
return
}

handleAuthResult(w, r, userProfile)
c.handleAuthResult(w, r, userProfile)
}

// TODO onboard user at key server and SDS: https://github.com/trustbloc/hub-auth/issues/38
Expand All @@ -342,9 +351,15 @@ 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, userProfile *user.Profile) {
handle := url.QueryEscape(uuid.New().String())

c.setUserProfile(handle, userProfile)

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 All @@ -359,6 +374,13 @@ func handleAuthError(w http.ResponseWriter, status int, msg string) {
}
}

func (c *Operation) setUserProfile(handle string, r *user.Profile) {
c.presDefsLock.Lock()
defer c.presDefsLock.Unlock()

c.userProfileRequest[handle] = r
}

// writeResponse writes interface value to response.
func (c *Operation) writeErrorResponse(rw http.ResponseWriter, status int, msg string) {
logger.Errorf(msg)
Expand Down

0 comments on commit a93ef61

Please sign in to comment.