Junior — Middle
What authentication and authorization mechanisms were used in your projects?
sobes.tech AI
Answer from AI
In Go projects, the following authentication and authorization mechanisms are often used:
- JWT (JSON Web Tokens) — for stateless authentication, where the server issues a token after successful login, and the client sends it in request headers.
- Basic Auth — a simple method of transmitting login and password in the header, usually for internal services.
- OAuth 2.0 — for integration with external providers (Google, Facebook), especially in web applications.
- Middleware for access rights verification — in Go, often implemented through middleware that checks the presence and validity of the token, as well as user permissions.
Example of simple middleware for JWT verification in Go:
func AuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tokenString := r.Header.Get("Authorization")
if tokenString == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// Add token verification logic here
// If the token is valid:
next.ServeHTTP(w, r)
})
}