|
| 1 | +# Enterprise Edition Integration Example |
| 2 | + |
| 3 | +This example demonstrates how to integrate and use the Tingly Box Enterprise Edition module with a simple Gin web service. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The example shows: |
| 8 | +- How to initialize the enterprise module |
| 9 | +- How to set up authentication middleware |
| 10 | +- How to protect routes with role-based access control |
| 11 | +- How to use the Integration interface for user and token management |
| 12 | + |
| 13 | +## Running the Example |
| 14 | + |
| 15 | +```bash |
| 16 | +# From the project root |
| 17 | +cd examples/enterprise |
| 18 | +go run main.go handlers.go |
| 19 | +``` |
| 20 | + |
| 21 | +The server will start on `http://localhost:12581` |
| 22 | + |
| 23 | +## API Endpoints |
| 24 | + |
| 25 | +### Public Endpoints (No Authentication) |
| 26 | + |
| 27 | +```bash |
| 28 | +# Health check |
| 29 | +curl http://localhost:12581/api/ping |
| 30 | + |
| 31 | +# Login (returns JWT access token) |
| 32 | +curl -X POST http://localhost:12581/api/auth/login \ |
| 33 | + -H "Content-Type: application/json" \ |
| 34 | + -d '{"username":"admin","password":"your-password"}' |
| 35 | + |
| 36 | +# Demo: Create a test user (not persisted) |
| 37 | +curl -X POST http://localhost:12581/api/auth/demo-create-user \ |
| 38 | + -H "Content-Type: application/json" \ |
| 39 | + -d '{"username":"testuser","password":"TestPass123","email":"test@example.com"}' |
| 40 | +``` |
| 41 | + |
| 42 | +### Protected Endpoints (Require Authentication) |
| 43 | + |
| 44 | +```bash |
| 45 | +# Get current user profile |
| 46 | +curl http://localhost:12581/api/profile \ |
| 47 | + -H "Authorization: Bearer YOUR_ACCESS_TOKEN" |
| 48 | + |
| 49 | +# Change password |
| 50 | +curl -X POST http://localhost:12581/api/change-password \ |
| 51 | + -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ |
| 52 | + -H "Content-Type: application/json" \ |
| 53 | + -d '{"current_password":"old","new_password":"NewPass123"}' |
| 54 | + |
| 55 | +# List my tokens |
| 56 | +curl http://localhost:12581/api/my-tokens \ |
| 57 | + -H "Authorization: Bearer YOUR_ACCESS_TOKEN" |
| 58 | + |
| 59 | +# Create a new API token |
| 60 | +curl -X POST http://localhost:12581/api/my-tokens \ |
| 61 | + -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ |
| 62 | + -H "Content-Type: application/json" \ |
| 63 | + -d '{"name":"My Token","scopes":["read:providers"]}' |
| 64 | + |
| 65 | +# Delete a token |
| 66 | +curl -X DELETE http://localhost:12581/api/my-tokens/TOKEN_UUID \ |
| 67 | + -H "Authorization: Bearer YOUR_ACCESS_TOKEN" |
| 68 | +``` |
| 69 | + |
| 70 | +### Admin Endpoints (Require Admin Role) |
| 71 | + |
| 72 | +```bash |
| 73 | +# List all users |
| 74 | +curl http://localhost:12581/api/admin/users \ |
| 75 | + -H "Authorization: Bearer YOUR_ADMIN_TOKEN" |
| 76 | + |
| 77 | +# Create a new user |
| 78 | +curl -X POST http://localhost:12581/api/admin/users \ |
| 79 | + -H "Authorization: Bearer YOUR_ADMIN_TOKEN" \ |
| 80 | + -H "Content-Type: application/json" \ |
| 81 | + -d '{"username":"newuser","email":"new@example.com","password":"Pass123","full_name":"New User","role":"user"}' |
| 82 | + |
| 83 | +# Get user by ID |
| 84 | +curl http://localhost:12581/api/admin/users/1 \ |
| 85 | + -H "Authorization: Bearer YOUR_ADMIN_TOKEN" |
| 86 | + |
| 87 | +# Update user |
| 88 | +curl -X PUT http://localhost:12581/api/admin/users/2 \ |
| 89 | + -H "Authorization: Bearer YOUR_ADMIN_TOKEN" \ |
| 90 | + -H "Content-Type: application/json" \ |
| 91 | + -d '{"full_name":"Updated Name","role":"admin"}' |
| 92 | + |
| 93 | +# Deactivate user |
| 94 | +curl -X POST http://localhost:12581/api/admin/users/2/deactivate \ |
| 95 | + -H "Authorization: Bearer YOUR_ADMIN_TOKEN" |
| 96 | + |
| 97 | +# Reset user password |
| 98 | +curl -X POST http://localhost:12581/api/admin/users/2/password \ |
| 99 | + -H "Authorization: Bearer YOUR_ADMIN_TOKEN" |
| 100 | + |
| 101 | +# List all tokens |
| 102 | +curl http://localhost:12581/api/admin/tokens \ |
| 103 | + -H "Authorization: Bearer YOUR_ADMIN_TOKEN" |
| 104 | + |
| 105 | +# Get system statistics |
| 106 | +curl http://localhost:12581/api/admin/stats \ |
| 107 | + -H "Authorization: Bearer YOUR_ADMIN_TOKEN" |
| 108 | +``` |
| 109 | + |
| 110 | +## Default Admin User |
| 111 | + |
| 112 | +On first run, the enterprise module creates a default admin user: |
| 113 | +- **Username:** `admin` |
| 114 | +- **Password:** `$CHANGE_REQUIRED$` (must be changed) |
| 115 | +- **Email:** `admin@tingly-box.local` |
| 116 | + |
| 117 | +You will need to implement the password change functionality to set a real password. |
| 118 | + |
| 119 | +## Integration Points |
| 120 | + |
| 121 | +### 1. Initialize the Enterprise Module |
| 122 | + |
| 123 | +```go |
| 124 | +integration := enterprise.NewIntegration() |
| 125 | + |
| 126 | +config := &enterprise.Config{ |
| 127 | + BaseDir: configDir, |
| 128 | + JWTSecret: "your-secret-key", |
| 129 | + AccessTokenExpiry: "15m", |
| 130 | + RefreshTokenExpiry: "168h", |
| 131 | + PasswordMinLength: 8, |
| 132 | + Logger: logrus.StandardLogger(), |
| 133 | +} |
| 134 | + |
| 135 | +if err := integration.Initialize(context.Background(), config); err != nil { |
| 136 | + log.Fatal(err) |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +### 2. Add Authentication Middleware |
| 141 | + |
| 142 | +```go |
| 143 | +// Protect routes with authentication |
| 144 | +protected := router.Group("/api") |
| 145 | +protected.Use(integration.AuthMiddleware()) |
| 146 | +{ |
| 147 | + protected.GET("/profile", handleProfile) |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### 3. Require Specific Roles |
| 152 | + |
| 153 | +```go |
| 154 | +admin := router.Group("/admin") |
| 155 | +admin.Use(integration.AuthMiddleware()) |
| 156 | +admin.Use(integration.RequireRole("admin")) |
| 157 | +{ |
| 158 | + admin.GET("/users", handleListUsers) |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +### 4. Use the Integration Interface |
| 163 | + |
| 164 | +```go |
| 165 | +func handleProfile(c *gin.Context) { |
| 166 | + userID := c.GetInt64("user_id") |
| 167 | + userInfo, err := integration.GetUserInfo(ctx, userID) |
| 168 | + // ... |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +## Architecture |
| 173 | + |
| 174 | +``` |
| 175 | +examples/enterprise/ |
| 176 | +├── main.go # Server setup and initialization |
| 177 | +├── handlers.go # HTTP handlers demonstrating Integration API usage |
| 178 | +└── README.md # This file |
| 179 | +``` |
| 180 | + |
| 181 | +## Key Files |
| 182 | + |
| 183 | +- **main.go:** Shows how to initialize the enterprise module and set up routes |
| 184 | +- **handlers.go:** Demonstrates how to use the Integration interface for common operations |
| 185 | + |
| 186 | +## Next Steps |
| 187 | + |
| 188 | +1. Implement the login endpoint with real authentication |
| 189 | +2. Implement user creation with database persistence |
| 190 | +3. Add password change functionality |
| 191 | +4. Integrate with your existing application routes |
| 192 | + |
| 193 | +## Documentation |
| 194 | + |
| 195 | +- [Integration Guide](../../docs/enterprise/INTEGRATION.md) |
| 196 | +- [Test Coverage](../../docs/enterprise/TEST_COVERAGE.md) |
| 197 | +- [Specification](../../docs/spec/20260207-enterprise-edition.md) |
0 commit comments