
OAuth 2.0 क्लाइंट लाइब्रेरी Kit अनुप्रयोगों के लिए जो प्राधिकरण कोड, PKCE, क्लाइंट क्रेडेंशियल्स, और रिफ्रेश टोकन प्रवाह को समर्थन करती है, साथ ही GitHub, Google, Microsoft, Discord, Slack, और GitLab के लिए अंतर्निहित प्रदाता प्रीसेट के साथ।
किट अनुप्रयोगों के लिए OAuth 2.0 क्लाइंट लाइब्रेरी
[TOC]
| फ़ाइल | विवरण |
|---|---|
.editorconfig | संपादक स्वरूपण कॉन्फ़िगरेशन |
.gitignore | बिल्ड आर्टिफैक्ट्स और निर्भरताओं के लिए Git इग्नोर नियम |
.tool-versions | asdf टूल संस्करण (Zig, Kit) |
LICENSE | MIT लाइसेंस फ़ाइल |
README.md | यह फ़ाइल |
examples/client-credentials.kit | ड्राई-रन क्लाइंट क्रेडेंशियल्स उदाहरण |
examples/github.kit | ड्राई-रन GitHub प्राधिकरण कोड उदाहरण |
kit.toml | मेटाडेटा, क्षमताओं और कार्यों के साथ पैकेज मैनिफेस्ट |
src/oauth.kit | OAuth 2.0 क्लाइंट कार्यान्वयन |
tests/oauth.test.kit | OAuth सहायकों के लिए कार्यात्मक परीक्षण |
tests/types.test.kit | प्रकार और स्थिरांक व्यवहार परीक्षण |
कोई Kit पैकेज निर्भरता नहीं।
यह पैकेज Kit के बिल्ट-इन HTTP और Time मॉड्यूल का उपयोग करता है। पैकेज मैनिफेस्ट net क्षमता का अनुरोध करता है क्योंकि टोकन एक्सचेंज और प्रमाणित अनुरोध सहायक HTTP कॉल करते हैं।
kit add gitlab.com/kit-lang/packages/kit-oauth.git
import Kit.Oauth as OAuth
main = fn =>
config = OAuth.github "client-id" "client-secret" "http://localhost:3000/callback" ["read:user", "user:email"]
state = OAuth.generate-state
auth-url = OAuth.authorize-url config state
println "Open this authorization URL:"
println auth-url
main
प्रदाता आपके कॉलबैक URL पर वापस रीडायरेक्ट करने के बाद, सत्यापित करें कि लौटाया गया state आपके द्वारा उत्पन्न स्टेट से मेल खाता है, फिर टोकन प्राप्त करने के लिए प्राधिकरण कोड का आदान-प्रदान करें:
match OAuth.exchange-code http-auth config code
| Ok tokens ->
println "Token type: ${tokens.token-type}"
println "Expires in: ${Int.to-string tokens.expires-in} seconds"
| Err err ->
println "OAuth error: ${err}"
डेस्कटॉप ऐप्स, मोबाइल ऐप्स और ब्राउज़र-आधारित फ़्लो जैसे सार्वजनिक क्लाइंट के लिए PKCE का उपयोग करें:
verifier = "abcdefghijklmnopqrstuvwxyz0123456789-._~abc"
state = OAuth.generate-state
auth-url = OAuth.authorize-url-pkce config state verifier
match OAuth.exchange-code-pkce http-auth config code verifier
| Ok tokens -> println "Access token received"
| Err err -> println "OAuth error: ${err}"
वर्तमान PKCE सहायक plain चैलेंज विधि का उपयोग करता है। इसे इस प्रकार संरचित किया गया है कि एक बार स्ट्रिंग SHA256 सहायक उपलब्ध होने पर यह S256 पर जा सके।
मशीन-टू-मशीन प्रमाणीकरण के लिए क्लाइंट क्रेडेंशियल्स का उपयोग करें:
config = OAuth.custom "client-id" "client-secret" "https://auth.example.com/oauth/authorize" "https://auth.example.com/oauth/token" "https://app.example.com/oauth/callback" ["api:read", "api:write"]
match OAuth.client-credentials http-auth config
| Ok tokens ->
println "Access token received"
| Err err ->
println "OAuth error: ${err}"
match OAuth.refresh-token http-auth config refresh-token
| Ok tokens -> println "Refreshed access token"
| Err err -> println "Refresh failed: ${err}"
match OAuth.get http-auth "https://api.example.com/user" tokens.access-token
| Ok response ->
println "Status: ${Int.to-string response.status}"
println response.body
| Error err ->
println "API error: ${err}"
json-body = "{\"name\":\"Kit\"}"
match OAuth.post http-auth "https://api.example.com/resources" json-body tokens.access-token
| Ok response -> println "Created: ${Int.to-string response.status}"
| Error err -> println "API error: ${err}"
बिल्ट-इन प्रदाता सहायक प्रदाता प्राधिकरण और टोकन एंडपॉइंट से भरा एक OAuthConfig लौटाते हैं:
state मान को मान्य करें।इंटरप्रेटर के साथ उदाहरण चलाएँ:
kit run examples/github.kit
kit run examples/client-credentials.kit
एक उदाहरण को नेटिव बाइनरी में संकलित करें:
kit build examples/github.kit && ./github
परीक्षण सूट चलाएँ:
kit test
कवरेज के साथ परीक्षण सूट चलाएँ:
kit test --coverage
मानक विकास कार्यप्रवाह चलाएँ:
kit dev
यह करेगा:
src/ में स्रोत फ़ाइलों की टाइप-जाँचexamples/ में उदाहरणों की टाइप-जाँचtests/ में परीक्षण चलानाउदाहरणों के लिए इंटरप्रेटर/कंपाइलर पैरिटी जाँच चलाएँ:
kit parity --failures-only
उदाहरण नियतात्मक आउटपुट का उपयोग करते हैं और लाइव नेटवर्क कॉल से बचते हैं ताकि पैरिटी इंटरप्रेटर और संकलित निष्पादन योग्य आउटपुट की विश्वसनीय तुलना कर सके।
डॉक कमेंट से API दस्तावेज़ीकरण उत्पन्न करें:
kit doc
नोट: डॉक कमेंट (##) वाले Kit स्रोत docs/*.html में HTML दस्तावेज़ उत्पन्न करेंगे।
जनरेट की गई फ़ाइलें, कैश, पैरिटी परिणाम, docs, लॉक फ़ाइलें और नेटिव बिल्ड आर्टिफैक्ट हटाएँ:
kit task clean
नोट: kit.toml में परिभाषित।
इस पैकेज को विकास के लिए स्थानीय रूप से स्थापित करने हेतु:
kit install
यह पैकेज को ~/.kit/packages/@kit/oauth/ में स्थापित करता है, जिससे यह अन्य प्रोजेक्ट्स में Kit.Oauth के रूप में आयात के लिए उपलब्ध हो जाता है।
यह पैकेज MIT लाइसेंस के अंतर्गत जारी किया गया है - विवरण के लिए LICENSE देखें।
| सहायक | प्रदाता |
|---|
OAuth.github | GitHub |
OAuth.google | |
OAuth.microsoft | Microsoft Entra ID / Azure AD |
OAuth.discord | Discord |
OAuth.slack | Slack |
OAuth.gitlab | GitLab |
OAuth.custom | कोई भी OAuth 2.0 प्रदाता |