Go (Golang) में प्राधिकरण और प्रमाणीकरण
Go में वेब एप्लिकेशन की सुरक्षा प्रमाणीकरण ("आप कौन हैं" की जाँच) और प्राधिकरण ("आप क्या कर सकते हैं" की जाँच) तंत्रों के सही कार्यान्वयन से शुरू होती है। इस लेख में हम तीन मुख्य दृष्टिकोणों पर चर्चा करेंगे: JWT (JSON Web Tokens), OAuth2 और सत्र प्रमाणीकरण। इनमें से प्रत्येक के अपने उपयोग के परिदृश्य, लाभ और कमियाँ हैं।
1. JWT (JSON Web Token) के साथ प्रमाणीकरण
JWT पक्षों के बीच JSON ऑब्जेक्ट के रूप में जानकारी स्थानांतरित करने का एक कॉम्पैक्ट और स्व-निहित तरीका है। Go में JWT के साथ काम करने के लिए अक्सर github.com/golang-jwt/jwt/v5 लाइब्रेरी का उपयोग किया जाता है।
स्थापना:
go get github.com/golang-jwt/jwt/v5JWT बनाने और सत्यापित करने का उदाहरण:
package main
import (\t"fmt"\t"time"\t"github.com/golang-jwt/jwt/v5")
var jwtSecret = []byte("my_secret_key")
// टोकन बनानाfunc createToken(userID string) (string, error) {\tclaims := jwt.MapClaims{\t\t"user_id": userID,\t\t"exp": time.Now().Add(24 * time.Hour).Unix(),\t}\ttoken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)\treturn token.SignedString(jwtSecret)}
// टोकन सत्यापित करनाfunc validateToken(tokenString string) (string, error) {\ttoken, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {\t\tif _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {\t\t\treturn nil, fmt.Errorf("अमान्य हस्ताक्षर विधि")\t\t}\t\treturn jwtSecret, nil\t})
\tif err != nil {\t\treturn "", err\t}
\tif claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {\t\treturn claims["user_id"].(string), nil\t}\treturn "", fmt.Errorf("अमान्य टोकन")}
func main() {\ttoken, _ := createToken("user123")\tfmt.Println("टोकन:", token)
\tuserID, err := validateToken(token)\tif err != nil {\t\tfmt.Println("त्रुटि:", err)\t} else {\t\tfmt.Println("उपयोगकर्ता:", userID)\t}}2. OAuth2 के माध्यम से प्रमाणीकरण
OAuth2 एक प्रतिनिधि पहुँच प्रोटोकॉल है जो उपयोगकर्ताओं को तृतीय-पक्ष सेवाओं (Google, GitHub, VK आदि) के माध्यम से लॉग इन करने की अनुमति देता है। Go में golang.org/x/oauth2 लाइब्रेरी लोकप्रिय है।
स्थापना:
go get golang.org/x/oauth2Google के साथ OAuth2 का उदाहरण:
package main
import (\t"context"\t"encoding/json"\t"fmt"\t"net/http"\t"golang.org/x/oauth2"\t"golang.org/x/oauth2/google")
var googleOauthConfig = &oauth2.Config{\tRedirectURL: "http://localhost:8080/callback",\tClientID: "your-client-id",\tClientSecret: "your-client-secret",\tScopes: []string{"https://www.googleapis.com/auth/userinfo.email"},\tEndpoint: google.Endpoint,}
func handleGoogleLogin(w http.ResponseWriter, r *http.Request) {\turl := googleOauthConfig.AuthCodeURL("state-token")\thttp.Redirect(w, r, url, http.StatusTemporaryRedirect)}
func handleCallback(w http.ResponseWriter, r *http.Request) {\tcode := r.URL.Query().Get("code")\ttoken, err := googleOauthConfig.Exchange(context.Background(), code)\tif err != nil {\t\thttp.Error(w, "टोकन विनिमय में त्रुटि", http.StatusInternalServerError)\t\treturn\t}
\t// उपयोगकर्ता डेटा प्राप्त करना\tclient := googleOauthConfig.Client(context.Background(), token)\tresp, _ := client.Get("https://www.googleapis.com/oauth2/v2/userinfo")\tdefer resp.Body.Close()
\tvar userInfo map[string]interface{}\tjson.NewDecoder(resp.Body).Decode(&userInfo)\tfmt.Fprintf(w, "ईमेल: %v", userInfo["email"])}
func main() {\thttp.HandleFunc("/auth/google", handleGoogleLogin)\thttp.HandleFunc("/callback", handleCallback)\thttp.ListenAndServe(":8080", nil)}3. सत्र प्रमाणीकरण
कुकी और सर्वर सत्रों का उपयोग करने वाला क्लासिक दृष्टिकोण। Go में अक्सर उपयोग किया जाता है