(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware)
| 73 | } |
| 74 | |
| 75 | func registerRoutes(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) { |
| 76 | // Public routes |
| 77 | r.POST("/login", authMiddleware.LoginHandler) |
| 78 | r.POST("/refresh", authMiddleware.RefreshHandler) |
| 79 | |
| 80 | // Public info endpoint |
| 81 | r.GET("/info", func(c *gin.Context) { |
| 82 | c.JSON(200, gin.H{ |
| 83 | "message": "Authorization Example API", |
| 84 | "users": gin.H{ |
| 85 | "admin": gin.H{"password": "admin", "role": "admin", "access": "All routes"}, |
| 86 | "user": gin.H{ |
| 87 | "password": "user", |
| 88 | "role": "user", |
| 89 | "access": "/user/* and /auth/profile", |
| 90 | }, |
| 91 | "guest": gin.H{"password": "guest", "role": "guest", "access": "/auth/hello only"}, |
| 92 | }, |
| 93 | "routes": gin.H{ |
| 94 | "public": []string{"/login", "/refresh", "/info"}, |
| 95 | "admin": []string{"/admin/users", "/admin/settings", "/admin/reports"}, |
| 96 | "user": []string{"/user/profile", "/user/settings"}, |
| 97 | "auth": []string{"/auth/hello", "/auth/profile", "/auth/logout"}, |
| 98 | }, |
| 99 | }) |
| 100 | }) |
| 101 | |
| 102 | // Admin routes - only admin role can access |
| 103 | adminRoutes := r.Group("/admin", authMiddleware.MiddlewareFunc()) |
| 104 | { |
| 105 | adminRoutes.GET("/users", adminUsersHandler) |
| 106 | adminRoutes.GET("/settings", adminSettingsHandler) |
| 107 | adminRoutes.GET("/reports", adminReportsHandler) |
| 108 | adminRoutes.POST("/users", createUserHandler) |
| 109 | adminRoutes.DELETE("/users/:id", deleteUserHandler) |
| 110 | } |
| 111 | |
| 112 | // User routes - user and admin roles can access |
| 113 | userRoutes := r.Group("/user", authMiddleware.MiddlewareFunc()) |
| 114 | { |
| 115 | userRoutes.GET("/profile", userProfileHandler) |
| 116 | userRoutes.PUT("/profile", updateProfileHandler) |
| 117 | userRoutes.GET("/settings", userSettingsHandler) |
| 118 | } |
| 119 | |
| 120 | // General auth routes - different permissions based on path |
| 121 | authRoutes := r.Group("/auth", authMiddleware.MiddlewareFunc()) |
| 122 | { |
| 123 | authRoutes.GET("/hello", helloHandler) // All authenticated users |
| 124 | authRoutes.GET("/profile", profileHandler) // User and admin only |
| 125 | authRoutes.POST("/logout", authMiddleware.LogoutHandler) // User Logout |
| 126 | authRoutes.GET("/whoami", whoAmIHandler) // All authenticated users |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func initParams() *jwt.GinJWTMiddleware { |
| 131 | return &jwt.GinJWTMiddleware{ |
no test coverage detected
searching dependent graphs…