|
| 1 | +# HTTP Authentication |
| 2 | +Authentication is a crucial aspect of web applications, controlling access to resources based on user roles or permissions. |
| 3 | +It is the process of verifying a user's identity to grant access to protected resources. It ensures only |
| 4 | +authenticated users can perform actions or access data within an application. |
| 5 | + |
| 6 | +GoFr offer various approaches to implement authorization. |
| 7 | + |
| 8 | +## 1. HTTP Basic Auth |
| 9 | +*Basic Authentication* is a simple HTTP authentication scheme where the user's credentials (username and password) are |
| 10 | +transmitted in the request header in a Base64-encoded format. |
| 11 | + |
| 12 | +Basic auth is the simplest way to authenticate your APIs. It's built on |
| 13 | +[HTTP protocol authentication scheme](https://datatracker.ietf.org/doc/html/rfc7617). It involves sending the term |
| 14 | +`Basic` trailed by the Base64-encoded `<username>:<password>` within the standard `Authorization` header. |
| 15 | + |
| 16 | +### Basic Authentication in GoFr |
| 17 | + |
| 18 | +GoFr offers two ways to implement basic authentication: |
| 19 | + |
| 20 | +**1. Predefined Credentials** |
| 21 | + |
| 22 | +Use `EnableBasicAuth(username, password)` to configure Gofr with pre-defined credentials. |
| 23 | + |
| 24 | +```go |
| 25 | +func main() { |
| 26 | + app := gofr.New() |
| 27 | + |
| 28 | + app.EnableBasicAuth("admin", "secret_password") // Replace with your credentials |
| 29 | + |
| 30 | + app.GET("/protected-resource", func(c *gofr.Context) (interface{}, error) { |
| 31 | + // Handle protected resource access |
| 32 | + return nil, nil |
| 33 | + }) |
| 34 | + |
| 35 | + app.Run() |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +**2. Custom Validation Function** |
| 40 | + |
| 41 | +Use `EnableBasicAuthWithFunc(validationFunc)` to implement your own validation logic for credentials. The `validationFunc` takes the username and password as arguments and returns true if valid, false otherwise. |
| 42 | + |
| 43 | +```go |
| 44 | +func validateUser(username string, password string) bool { |
| 45 | + // Implement your credential validation logic here |
| 46 | + // This example uses hardcoded credentials for illustration only |
| 47 | + return username == "john" && password == "doe123" |
| 48 | +} |
| 49 | + |
| 50 | +func main() { |
| 51 | + app := gofr.New() |
| 52 | + |
| 53 | + app.EnableBasicAuthWithFunc(validateUser) |
| 54 | + |
| 55 | + app.GET("/secure-data", func(c *gofr.Context) (interface{}, error) { |
| 56 | + // Handle access to secure data |
| 57 | + return nil, nil |
| 58 | + }) |
| 59 | + |
| 60 | + app.Run() |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Adding Basic Authentication to HTTP Services |
| 65 | + |
| 66 | +This code snippet demonstrates how to add basic authentication to an HTTP service in GoFr and make a request with the appropriate Authorization header: |
| 67 | + |
| 68 | +```go |
| 69 | +app.AddHTTPService("order", "https://localhost:2000", |
| 70 | + &service.Authentication{UserName: "abc", Password: "pass"}, |
| 71 | +) |
| 72 | +``` |
| 73 | + |
| 74 | +## 2. API Keys Auth |
| 75 | +Users include a unique API key in the request header for validation against a store of authorized keys. |
| 76 | + |
| 77 | +### Usage: |
| 78 | +GoFr offers two ways to implement API Keys authentication. |
| 79 | + |
| 80 | +**1. Framework Default Validation** |
| 81 | +- Users can select the framework's default validation using **_EnableAPIKeyAuth(apiKeys ...string)_** |
| 82 | + |
| 83 | +```go |
| 84 | +package main |
| 85 | + |
| 86 | +func main() { |
| 87 | + // initialise gofr object |
| 88 | + app := gofr.New() |
| 89 | + |
| 90 | + app.EnableAPIKeyAuth("9221e451-451f-4cd6-a23d-2b2d3adea9cf", "0d98ecfe-4677-48aa-b463-d43505766915") |
| 91 | + |
| 92 | + app.GET("/customer", Customer) |
| 93 | + |
| 94 | + app.Run() |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +**2. Custom Validation Function** |
| 99 | +- Users can create their own validator function `apiKeyValidator(apiKey string) bool` for validating APIKeys and pass the func in **_EnableAPIKeyAuthWithFunc(validator)_** |
| 100 | + |
| 101 | +```go |
| 102 | +package main |
| 103 | + |
| 104 | +func apiKeyValidator(apiKey string) bool { |
| 105 | + validKeys := []string{"f0e1dffd-0ff0-4ac8-92a3-22d44a1464e4", "d7e4b46e-5b04-47b2-836c-2c7c91250f40"} |
| 106 | + |
| 107 | + return slices.Contains(validKeys, apiKey) |
| 108 | +} |
| 109 | + |
| 110 | +func main() { |
| 111 | + // initialise gofr object |
| 112 | + app := gofr.New() |
| 113 | + |
| 114 | + app.EnableAPIKeyAuthWithFunc(apiKeyValidator) |
| 115 | + |
| 116 | + app.GET("/customer", Customer) |
| 117 | + |
| 118 | + app.Run() |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### Adding API-KEY Authentication to HTTP Services |
| 123 | +This code snippet demonstrates how to add API Key authentication to an HTTP service in GoFr and make a request with the appropriate Authorization header: |
| 124 | + |
| 125 | +```go |
| 126 | +app.AddHTTPService("http-server-using-redis", "http://localhost:8000", &service.APIKeyConfig{APIKey: "9221e451-451f-4cd6-a23d-2b2d3adea9cf"}) |
| 127 | +``` |
| 128 | + |
| 129 | +## 3. OAuth 2.0 |
| 130 | +OAuth 2.0 is the industry-standard protocol for authorization. |
| 131 | +It focuses on client developer simplicity while providing specific authorization flows for web applications, desktop applications, mobile phones, and living room devices. |
| 132 | +To know more about it refer [here](https://www.rfc-editor.org/rfc/rfc6749) |
| 133 | + |
| 134 | +It involves sending the term `Bearer` trailed by the encoded token within the standard `Authorization` header. |
| 135 | + |
| 136 | +### OAuth Authentication in GoFr |
| 137 | + |
| 138 | +GoFr supports authenticating tokens encoded by algorithm `RS256/384/512`. |
| 139 | + |
| 140 | +### App level Authentication |
| 141 | +Enable OAuth 2.0 with three-legged flow to authenticate requests |
| 142 | + |
| 143 | +Use `EnableOAuth(jwks-endpoint,refresh_interval)` to configure Gofr with pre-defined credentials. |
| 144 | + |
| 145 | +```go |
| 146 | +func main() { |
| 147 | + app := gofr.New() |
| 148 | + |
| 149 | + app.EnableOAuth("http://jwks-endpoint", 20) |
| 150 | + |
| 151 | + app.GET("/protected-resource", func(c *gofr.Context) (interface{}, error) { |
| 152 | + // Handle protected resource access |
| 153 | + return nil, nil |
| 154 | + }) |
| 155 | + |
| 156 | + app.Run() |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +### Adding OAuth Authentication to HTTP Services |
| 161 | +For server-to-server communication it follows two-legged OAuth, also known as "client credentials" flow, |
| 162 | +where the client application directly exchanges its own credentials (ClientID and ClientSecret) |
| 163 | +for an access token without involving any end-user interaction. |
| 164 | + |
| 165 | +This code snippet demonstrates how two-legged OAuth authentication is added to an HTTP service in GoFr and make a request with the appropriate Authorization header. |
| 166 | + |
| 167 | +```go |
| 168 | +a.AddHTTPService("orders", "http://localhost:9000", |
| 169 | + &service.OAuthConfig{ // Replace with your credentials |
| 170 | + ClientID: "0iyeGcLYWudLGqZfD6HvOdZHZ5TlciAJ", |
| 171 | + ClientSecret: "GQXTY2f9186nUS3C9WWi7eJz8-iVEsxq7lKxdjfhOJbsEPPtEszL3AxFn8k_NAER", |
| 172 | + TokenURL: "https://dev-zq6tvaxf3v7p0g7j.us.auth0.com/oauth/token", |
| 173 | + Scopes: []string{"read:order"}, |
| 174 | + EndpointParams: map[string][]string{ |
| 175 | + "audience": {"https://dev-zq6tvaxf3v7p0g7j.us.auth0.com/api/v2/"}, |
| 176 | + }, |
| 177 | +}) |
| 178 | +``` |
0 commit comments