Echo Quick Start Guide by J. Ben Huson
Author:J. Ben Huson
Language: eng
Format: epub
Tags: COM060160 - COMPUTERS / Web / Web Programming, COM060080 - COMPUTERS / Web / General, COM060180 - COMPUTERS / Web / Web Services and APIs
Publisher: Packt Publishing
Published: 2018-05-30T05:48:39+00:00
In action
Within the Echo project, there is a directory called middleware, which contains many contributed middleware solutions seen here: https://github.com/labstack/echo/tree/master/middleware. These middleware functions have been vetted by the community, and follow the middleware best practices guidelines. In this section, we will dissect one very useful middleware, and show how to use these middleware in our example application.
We will start by looking at middleware.JWT, which is a very helpful middleware that takes a JSON Web Token (JWT) from the request header specified by the developer and validates that the token is legitimate. In our example, the handlers.Login handler will validate the user credentials with bcrypt, and after that verification, we will create a JWT for the caller to insert into their request headers. The following is the JWT creation code located in $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter4/handlers/login.go:
// need to make a token, successful login signingKey := c.Get(models.SigningContextKey).([]byte) // Create the Claims claims := &jwt.StandardClaims{ ExpiresAt: time.Now().Add(time.Hour * 72).Unix(), Issuer: "service", } token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) ss, err := token.SignedString(signingKey) if err != nil { resp.Success = false resp.Message = "Server Error" return c.JSON(http.StatusInternalServerError, resp) } resp.Token = ss return c.JSON(http.StatusOK, resp)
After we have this JWT token created, on successful login, our client can attach this token to the Authentication request header with the Bearer authentication scheme for subsequent requests. Taking our example back to inserting the middleware, the following inside $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter4/cmd/service/main.go will allow us to insert the JWT middleware that comes with Echo in order to validate (for the specific group of routes denoted) that the request contains the correct authentication information:
// Latest Reminder Routes reminderGroup := e.Group("/reminder") reminderGroup.Use(middleware.JWT(signingKey))
As you can see, we are assigning our /reminder group, which includes all of the API endpoints for reminder manipulation and retrieval to use the Echo-contributed middleware.JWT middleware. This change has the effect that every single route that is within the reminder group will have the JWT authentication middleware applied, which performs this code to validate the JWT from the token passed in the request:
token, err = jwt.ParseWithClaims(auth, claims, config.keyFunc) if err == nil && token.Valid { // Store user information from token into context. c.Set(config.ContextKey, token) return next(c) }
This allows our handlers to remain completely ignorant of the fact that we need to perform authentication, causing our routes to be less complex and easier to read.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6552)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6443)
Kotlin in Action by Dmitry Jemerov(5090)
Odoo 15 Development Essentials - Fifth Edition by Daniel Reis & Greg Mader(3495)
Odoo 15 Development Essentials by Daniel Reis(2834)
React Native - Building Mobile Apps with JavaScript by Novick Vladimir(2551)
Learning Angular - Second Edition by Christoffer Noring(2380)
Pride and Prejudice by Jane Austen(2366)
Mobile Forensics Cookbook by Igor Mikhaylov(2039)
Computers For Seniors For Dummies by Nancy C. Muir(2023)
Bulletproof Android: Practical Advice for Building Secure Apps (Developer's Library) by Godfrey Nolan(1893)
Android Development with Kotlin by Marcin Moskala & Igor Wojda(1817)
Building Android UIs with Custom Views by Raimon Ràfols Montané(1809)
1936941139 (N) by Bob Rosenthal(1753)
Building Progressive Web Apps: Bringing the Power of Native to the Browser by Ater Tal(1748)
Hands-On Internet of Things with MQTT by Tim Pulver(1728)
Android App Development by Franceschi Hervé J.;(1726)
Ember.js in Action by Joachim Haagen Skeie(1708)
Hands-On Design Patterns with React Native by Mateusz Grzesiukiewicz(1666)
