Skip to content

Commit

Permalink
Adds an API to refresh User access token
Browse files Browse the repository at this point in the history
This adds an API to refresh access token for user. This requires user
refresh token to be passed to get a new access token.

Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
  • Loading branch information
SM43 committed Feb 22, 2021
1 parent 2a987f9 commit a05a0f0
Show file tree
Hide file tree
Showing 29 changed files with 2,098 additions and 59 deletions.
34 changes: 34 additions & 0 deletions api/cmd/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ import (
resourcesvr "github.com/tektoncd/hub/api/gen/http/resource/server"
statussvr "github.com/tektoncd/hub/api/gen/http/status/server"
swaggersvr "github.com/tektoncd/hub/api/gen/http/swagger/server"
usersvr "github.com/tektoncd/hub/api/gen/http/user/server"
"github.com/tektoncd/hub/api/gen/log"
rating "github.com/tektoncd/hub/api/gen/rating"
resource "github.com/tektoncd/hub/api/gen/resource"
status "github.com/tektoncd/hub/api/gen/status"
<<<<<<< HEAD
v1resourcesvr "github.com/tektoncd/hub/api/v1/gen/http/resource/server"
v1swaggersvr "github.com/tektoncd/hub/api/v1/gen/http/swagger/server"
v1resource "github.com/tektoncd/hub/api/v1/gen/resource"
=======
user "github.com/tektoncd/hub/api/gen/user"
>>>>>>> 2143c2a (Adds an API to refresh User access token)
)

// handleHTTPServer starts configures and starts a HTTP server on the given
Expand All @@ -59,6 +64,7 @@ func handleHTTPServer(
resourceEndpoints *resource.Endpoints,
v1resourceEndpoints *v1resource.Endpoints,
statusEndpoints *status.Endpoints,
userEndpoints *user.Endpoints,
wg *sync.WaitGroup, errc chan error, logger *log.Logger, debug bool) {

// Setup goa log adapter.
Expand Down Expand Up @@ -90,6 +96,7 @@ func handleHTTPServer(
// the service input and output data structures to HTTP requests and
// responses.
var (
<<<<<<< HEAD
adminServer *adminsvr.Server
authServer *authsvr.Server
catalogServer *catalogsvr.Server
Expand All @@ -100,6 +107,17 @@ func handleHTTPServer(
statusServer *statussvr.Server
swaggerServer *swaggersvr.Server
v1swaggerServer *v1swaggersvr.Server
=======
adminServer *adminsvr.Server
authServer *authsvr.Server
catalogServer *catalogsvr.Server
categoryServer *categorysvr.Server
ratingServer *ratingsvr.Server
resourceServer *resourcesvr.Server
statusServer *statussvr.Server
swaggerServer *swaggersvr.Server
userServer *usersvr.Server
>>>>>>> 2143c2a (Adds an API to refresh User access token)
)
{
eh := errorHandler(logger)
Expand All @@ -112,7 +130,11 @@ func handleHTTPServer(
v1resourceServer = v1resourcesvr.New(v1resourceEndpoints, mux, dec, enc, eh, nil)
statusServer = statussvr.New(statusEndpoints, mux, dec, enc, eh, nil)
swaggerServer = swaggersvr.New(nil, mux, dec, enc, eh, nil)
<<<<<<< HEAD
v1swaggerServer = v1swaggersvr.New(nil, mux, dec, enc, eh, nil)
=======
userServer = usersvr.New(userEndpoints, mux, dec, enc, eh, nil)
>>>>>>> 2143c2a (Adds an API to refresh User access token)

if debug {
servers := goahttp.Servers{
Expand All @@ -125,7 +147,11 @@ func handleHTTPServer(
v1resourceServer,
statusServer,
swaggerServer,
<<<<<<< HEAD
v1swaggerServer,
=======
userServer,
>>>>>>> 2143c2a (Adds an API to refresh User access token)
}
servers.Use(httpmdlwr.Debug(mux, os.Stdout))
}
Expand All @@ -140,7 +166,11 @@ func handleHTTPServer(
v1resourcesvr.Mount(mux, v1resourceServer)
statussvr.Mount(mux, statusServer)
swaggersvr.Mount(mux, swaggerServer)
<<<<<<< HEAD
v1swaggersvr.Mount(mux, v1swaggerServer)
=======
usersvr.Mount(mux, userServer)
>>>>>>> 2143c2a (Adds an API to refresh User access token)

// Wrap the multiplexer with additional middlewares. Middlewares mounted
// here apply to all the service endpoints.
Expand Down Expand Up @@ -180,7 +210,11 @@ func handleHTTPServer(
for _, m := range swaggerServer.Mounts {
logger.Infof("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}
<<<<<<< HEAD
for _, m := range v1swaggerServer.Mounts {
=======
for _, m := range userServer.Mounts {
>>>>>>> 2143c2a (Adds an API to refresh User access token)
logger.Infof("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}

Expand Down
30 changes: 30 additions & 0 deletions api/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
rating "github.com/tektoncd/hub/api/gen/rating"
resource "github.com/tektoncd/hub/api/gen/resource"
status "github.com/tektoncd/hub/api/gen/status"
user "github.com/tektoncd/hub/api/gen/user"
"github.com/tektoncd/hub/api/pkg/app"
"github.com/tektoncd/hub/api/pkg/db/initializer"
adminsvc "github.com/tektoncd/hub/api/pkg/service/admin"
Expand All @@ -41,8 +42,12 @@ import (
ratingsvc "github.com/tektoncd/hub/api/pkg/service/rating"
resourcesvc "github.com/tektoncd/hub/api/pkg/service/resource"
statussvc "github.com/tektoncd/hub/api/pkg/service/status"
<<<<<<< HEAD
v1resource "github.com/tektoncd/hub/api/v1/gen/resource"
v1resourcesvc "github.com/tektoncd/hub/api/v1/service/resource"
=======
usersvc "github.com/tektoncd/hub/api/pkg/service/user"
>>>>>>> 2143c2a (Adds an API to refresh User access token)
)

func main() {
Expand Down Expand Up @@ -81,6 +86,7 @@ func main() {

// Initialize the services.
var (
<<<<<<< HEAD
adminSvc admin.Service
authSvc auth.Service
catalogSvc catalog.Service
Expand All @@ -89,6 +95,16 @@ func main() {
resourceSvc resource.Service
v1resourceSvc v1resource.Service
statusSvc status.Service
=======
adminSvc admin.Service
authSvc auth.Service
catalogSvc catalog.Service
categorySvc category.Service
ratingSvc rating.Service
resourceSvc resource.Service
statusSvc status.Service
userSvc user.Service
>>>>>>> 2143c2a (Adds an API to refresh User access token)
)
{
adminSvc = adminsvc.New(api)
Expand All @@ -99,11 +115,13 @@ func main() {
resourceSvc = resourcesvc.New(api)
v1resourceSvc = v1resourcesvc.New(api)
statusSvc = statussvc.New(api)
userSvc = usersvc.New(api)
}

// Wrap the services in endpoints that can be invoked from other services
// potentially running in different processes.
var (
<<<<<<< HEAD
adminEndpoints *admin.Endpoints
authEndpoints *auth.Endpoints
catalogEndpoints *catalog.Endpoints
Expand All @@ -112,6 +130,16 @@ func main() {
resourceEndpoints *resource.Endpoints
v1resourceEndpoints *v1resource.Endpoints
statusEndpoints *status.Endpoints
=======
adminEndpoints *admin.Endpoints
authEndpoints *auth.Endpoints
catalogEndpoints *catalog.Endpoints
categoryEndpoints *category.Endpoints
ratingEndpoints *rating.Endpoints
resourceEndpoints *resource.Endpoints
statusEndpoints *status.Endpoints
userEndpoints *user.Endpoints
>>>>>>> 2143c2a (Adds an API to refresh User access token)
)
{
adminEndpoints = admin.NewEndpoints(adminSvc)
Expand All @@ -122,6 +150,7 @@ func main() {
resourceEndpoints = resource.NewEndpoints(resourceSvc)
v1resourceEndpoints = v1resource.NewEndpoints(v1resourceSvc)
statusEndpoints = status.NewEndpoints(statusSvc)
userEndpoints = user.NewEndpoints(userSvc)
}

// Create channel used by both the signal handler and server goroutines
Expand Down Expand Up @@ -171,6 +200,7 @@ func main() {
resourceEndpoints,
v1resourceEndpoints,
statusEndpoints,
userEndpoints,
&wg, errc, api.Logger("http"), *dbgF,
)
}
Expand Down
1 change: 1 addition & 0 deletions api/design/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var _ = API("hub", func() {
"resource",
"status",
"swagger",
"user",
)
})

Expand Down
6 changes: 6 additions & 0 deletions api/design/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ var JWTAuth = JWTSecurity("jwt", func() {
Scope("agent:create", "Access to create or update an agent")
Scope("catalog:refresh", "Access to refresh catalog")
Scope("config:refresh", "Access to refresh config file")
Scope("refresh:token", "Access to refresh user access token")
})

var HubService = Type("HubService", func() {
Expand Down Expand Up @@ -357,3 +358,8 @@ var AuthTokens = Type("AuthTokens", func() {
Attribute("access", Token, "Access Token")
Attribute("refresh", Token, "Refresh Token")
})

var AccessToken = Type("AccessToken", func() {
Description("Access Token for User")
Attribute("access", Token, "Access Token for user")
})
57 changes: 57 additions & 0 deletions api/design/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright © 2021 The Tekton Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package design

import (
"github.com/tektoncd/hub/api/design/types"
. "goa.design/goa/v3/dsl"
)

var _ = Service("user", func() {
Description("The user service exposes endpoint to get user specific specs")

Error("invalid-token", ErrorResult, "Invalid User token")
Error("invalid-scopes", ErrorResult, "Invalid User scope")
Error("internal-error", ErrorResult, "Internal Server Error")

Method("RefreshAccessToken", func() {
Description("Refresh the access token of User")
Security(types.JWTAuth, func() {
Scope("refresh:token")
})
Payload(func() {
Token("refreshToken", String, "Refresh Token of User", func() {
Example("refreshToken", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."+
"eyJleHAiOjE1Nzc4ODM2MDAsImlhdCI6MTU3Nzg4MDAwMCwiaWQiOjExLCJpc3MiOiJUZWt0b24gSHViIiwic2NvcGVzIjpbInJlZnJlc2g6dG9rZW4iXSwidHlwZSI6InJlZnJlc2gtdG9rZW4ifQ."+
"4RdUk5ttHdDiymurlZ_f7Uy5Pas3Lq9w04BjKQKRiCE")
})
Required("refreshToken")
})
Result(func() {
Attribute("data", types.AccessToken, "User Access JWT")
Required("data")
})

HTTP(func() {
POST("/user/refresh/accesstoken")
Header("refreshToken:Authorization")

Response(StatusOK)
Response("internal-error", StatusInternalServerError)
Response("invalid-token", StatusUnauthorized)
Response("invalid-scopes", StatusForbidden)
})
})
})
4 changes: 2 additions & 2 deletions api/gen/admin/endpoints.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/gen/catalog/endpoints.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a05a0f0

Please sign in to comment.