Skip to content

Commit

Permalink
Add (Docs) -> Added comment for docs and re-generated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shib committed Mar 23, 2022
1 parent fa5fae2 commit f2e9def
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 17 deletions.
64 changes: 57 additions & 7 deletions auth.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import crypto.rand
import crypto.sha256
import crypto.sha512

// Represent a hash method used in digest auth.
pub enum HashMethod {
md5
md5_sess
Expand All @@ -18,6 +19,12 @@ pub enum HashMethod {
unknown
}

// Return a string which represent a hash method from enum value.
// Example:
// ```v
// enum_hash := HashMethod.md5
// str_hash := enum_hash.str()
// ```
pub fn (h HashMethod) str() string {
match h {
.md5 { return 'MD5' }
Expand All @@ -30,6 +37,12 @@ pub fn (h HashMethod) str() string {
}
}

// Retrurn a HashMethod enum value from a given string.
// Example:
// ```v
// str_hash := "MD5-sess"
// enum_hash := hash_method_from_str(str_hash)
// ```
pub fn hash_method_from_str(withValue string) HashMethod {
match withValue {
'MD5-sess' { return .md5_sess }
Expand All @@ -42,12 +55,19 @@ pub fn hash_method_from_str(withValue string) HashMethod {
}
}

// Respresent a quality of prodtection used in digest auth.
pub enum Qop {
auth
auth_int
unknown
}

// Return a string which represent a qop from enum value.
// Example:
// ```v
// enum_qop := Qup.auth
// str_qop := enum_qop.str()
// ```
pub fn (q Qop) str() string {
match q {
.auth { return 'auth' }
Expand All @@ -56,6 +76,12 @@ pub fn (q Qop) str() string {
}
}

// Retrurn a qop enum value from a given string.
// Example:
// ```v
// str_qop := "auth"
// enum_qop := qop_from_str(str_qop)
// ```
pub fn qop_from_str(withValue string) Qop {
match withValue {
'auth' { return .auth }
Expand All @@ -64,6 +90,7 @@ pub fn qop_from_str(withValue string) Qop {
}
}

// Main struct which contains data for the requests.
pub struct DigestAuthentification {
pub mut:
username string
Expand All @@ -81,15 +108,16 @@ pub mut:
cnonce string
}

// Control if the required field to perforn the minimal digest auth is present after the challenge request.
fn (d DigestAuthentification) have_required_fields() bool {
if d.algorithm == .unknown || d.realm.len == 0 || d.nonce.len == 0 {
return false
}
return true
}

// Return a hex hashed based with the desired hash method for a given string.
fn (d DigestAuthentification) hash(withValue string) string {
// dump(withValue)
match d.algorithm {
.md5 {
return md5.hexhash(withValue)
Expand All @@ -115,10 +143,9 @@ fn (d DigestAuthentification) hash(withValue string) string {
}
}

// Parse the www_authenticate header field and fill the DigestAuthentification with value returned by the challenge request.
fn (mut d DigestAuthentification) parse_www_authentificate_header(withValue string) {

a := 'Digest qop="auth",algorithm=MD5,realm="monero-rpc",nonce="mXGqDmo9MY6hFgDK0kT/sw==",stale=true'
without_digest := a.all_after('Digest ')
without_digest := withValue.all_after('Digest ')
raw_fields := without_digest.split(',')
fields := raw_fields.map(fn (s string) []string {
mut parsed_res := []string{}
Expand Down Expand Up @@ -158,11 +185,13 @@ fn (mut d DigestAuthentification) parse_www_authentificate_header(withValue stri
}
}

// Return a client nonce.
fn (mut d DigestAuthentification) generate_cnonce() {
// FIXME: Not very secure ^^
d.cnonce = base64.encode_str(md5.hexhash(rand.int_u64(667670123) or { 1 }.str()))
}

// Try to solve the challenge and return the formated authorization header field
fn (mut d DigestAuthentification) build_challenge_solution() ?string {
if d.qop != .unknown {
d.generate_cnonce()
Expand Down Expand Up @@ -222,9 +251,20 @@ fn (mut d DigestAuthentification) build_challenge_solution() ?string {
return auth
}

// Fetch from an endpoint which require a digest auth.
// Example:
// ```v
// mut config := http.FetchConfig{
// url: 'https://httpbin.org/digest-auth/auth/user/pass/MD5'
// method: .get
// }
//
// mut d_auth := new_digest_authentification('user', 'pass', config) or { return }
// mut rsp := d_auth.fetch() or { panic('$err') }
// ```
pub fn (mut d DigestAuthentification) fetch() ?http.Response {
d.request.header.delete(.connection)
d.request.header.add(.connection, "Keep-Alive")
d.request.header.add(.connection, 'Keep-Alive')

req_challenge := http.fetch(d.request) or {
return error('Error while reqesting auth challenge: $err')
Expand Down Expand Up @@ -257,15 +297,25 @@ pub fn (mut d DigestAuthentification) fetch() ?http.Response {
}

d.request.header.add(.authorization, challenge_solution)
/*
/*
// retrying request with solution
resp := http.fetch(d.request) or {
return error('Error while proceeding request with solution: $err')
}
*/
*/
return none
}

// Return a DigestAuthentification used to fetch from a digest auth protected endpoint
// Example:
// ```v
// mut config := http.FetchConfig{
// url: 'https://httpbin.org/digest-auth/auth/user/pass/MD5'
// method: .get
// }
//
// mut d_auth := new_digest_authentification('user', 'pass', config) or { return }
// ```
pub fn new_digest_authentification(withUsername string, andPassword string, forFetchConfig http.FetchConfig) ?DigestAuthentification {
mut d_auth := DigestAuthentification{
username: withUsername
Expand Down
50 changes: 41 additions & 9 deletions docs/vigest_auth.html → docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@
<div class="info">
<div class="module">vigest_auth</div>
<div class="toggle-version-container">
<span>0.0.1 1d83ab6</span>
<span>0.0.3 1d83ab6</span>
<div id="dark-mode-toggle" role="switch" aria-checked="false" aria-label="Toggle dark mode"><svg xmlns="http://www.w3.org/2000/svg" class="light-icon" height="24" viewBox="0 0 24 24" width="24"><path d="M0 0h24v24H0z" fill="none"/><path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/></svg>
<svg xmlns="http://www.w3.org/2000/svg" class="dark-icon" enable-background="new 0 0 24 24" height="24" viewBox="0 0 24 24" width="24"><g><rect fill="none" height="24" width="24"/></g><g><g><g><path d="M11.1,12.08C8.77,7.57,10.6,3.6,11.63,2.01C6.27,2.2,1.98,6.59,1.98,12c0,0.14,0.02,0.28,0.02,0.42 C2.62,12.15,3.29,12,4,12c1.66,0,3.18,0.83,4.1,2.15C9.77,14.63,11,16.17,11,18c0,1.52-0.87,2.83-2.12,3.51 c0.98,0.32,2.03,0.5,3.11,0.5c3.5,0,6.58-1.8,8.37-4.52C18,17.72,13.38,16.52,11.1,12.08z"/></g><path d="M7,16l-0.18,0C6.4,14.84,5.3,14,4,14c-1.66,0-3,1.34-3,3s1.34,3,3,3c0.62,0,2.49,0,3,0c1.1,0,2-0.9,2-2 C9,16.9,8.1,16,7,16z"/></g></g></svg>
</div>
Expand Down Expand Up @@ -985,17 +985,32 @@

<section id="hash_method_from_str" class="doc-node">
<div class="title"><h2>fn hash_method_from_str <a href="#hash_method_from_str">#</a></h2></div><pre class="signature"><code><span class="token keyword">fn</span> <span class="token function">hash_method_from_str</span><span class="token punctuation">(</span>withValue <span class="token builtin">string</span><span class="token punctuation">)</span> HashMethod</code></pre>

<p>Retrurn a HashMethod enum value from a given string.<br>
Example:</p>
<pre><code class="language-v">str_hash := &quot;MD5-sess&quot;
enum_hash := hash_method_from_str(str_hash)
</code></pre>

</section>
<section id="new_digest_authentification" class="doc-node">
<div class="title"><h2>fn new_digest_authentification <a href="#new_digest_authentification">#</a></h2></div><pre class="signature"><code><span class="token keyword">fn</span> <span class="token function">new_digest_authentification</span><span class="token punctuation">(</span>withUsername <span class="token builtin">string</span><span class="token punctuation">,</span> andPassword <span class="token builtin">string</span><span class="token punctuation">,</span> forFetchConfig http<span class="token punctuation">.</span>FetchConfig<span class="token punctuation">)</span> ?DigestAuthentification</code></pre>
<p>Return a DigestAuthentification used to fetch from a digest auth protected endpoint Example:</p>
<pre><code class="language-v">mut config := http.FetchConfig{
url: 'https://httpbin.org/digest-auth/auth/user/pass/MD5'
method: .get
}

mut d_auth := new_digest_authentification('user', 'pass', config) or { return }
</code></pre>

</section>
<section id="qop_from_str" class="doc-node">
<div class="title"><h2>fn qop_from_str <a href="#qop_from_str">#</a></h2></div><pre class="signature"><code><span class="token keyword">fn</span> <span class="token function">qop_from_str</span><span class="token punctuation">(</span>withValue <span class="token builtin">string</span><span class="token punctuation">)</span> Qop</code></pre>

<p>Retrurn a qop enum value from a given string.<br>
Example:</p>
<pre><code class="language-v">str_qop := &quot;auth&quot;
enum_qop := qop_from_str(str_qop)
</code></pre>

</section>
<section id="HashMethod" class="doc-node">
Expand All @@ -1008,12 +1023,16 @@
sha512_256_sess
unknown
<span class="token punctuation">}</span></code></pre>

<p>Represent a hash method used in digest auth.</p>

</section>
<section id="HashMethod.str" class="doc-node">
<div class="title"><h2>fn (HashMethod) str <a href="#HashMethod.str">#</a></h2></div><pre class="signature"><code><span class="token keyword">fn</span> <span class="token punctuation">(</span>h HashMethod<span class="token punctuation">)</span> <span class="token function">str</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span></code></pre>

<p>Return a string which represent a hash method from enum value.<br>
Example:</p>
<pre><code class="language-v">enum_hash := HashMethod.md5
str_hash := enum_hash.str()
</code></pre>

</section>
<section id="Qop" class="doc-node">
Expand All @@ -1022,12 +1041,16 @@
auth_int
unknown
<span class="token punctuation">}</span></code></pre>

<p>Respresent a quality of prodtection used in digest auth.</p>

</section>
<section id="Qop.str" class="doc-node">
<div class="title"><h2>fn (Qop) str <a href="#Qop.str">#</a></h2></div><pre class="signature"><code><span class="token keyword">fn</span> <span class="token punctuation">(</span>q Qop<span class="token punctuation">)</span> <span class="token function">str</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span></code></pre>

<p>Return a string which represent a qop from enum value.<br>
Example:</p>
<pre><code class="language-v">enum_qop := Qup.auth
str_qop := enum_qop.str()
</code></pre>

</section>
<section id="DigestAuthentification" class="doc-node">
Expand All @@ -1047,17 +1070,26 @@
nc <span class="token builtin">int</span>
cnonce <span class="token builtin">string</span>
<span class="token punctuation">}</span></code></pre>

<p>Main struct which contains data for the requests.</p>

</section>
<section id="DigestAuthentification.fetch" class="doc-node">
<div class="title"><h2>fn (DigestAuthentification) fetch <a href="#DigestAuthentification.fetch">#</a></h2></div><pre class="signature"><code><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword">mut</span> d DigestAuthentification<span class="token punctuation">)</span> <span class="token function">fetch</span><span class="token punctuation">(</span><span class="token punctuation">)</span> ?http<span class="token punctuation">.</span>Response</code></pre>
<p>Fetch from an endpoint which require a digest auth.<br>
Example:</p>
<pre><code class="language-v">mut config := http.FetchConfig{
url: 'https://httpbin.org/digest-auth/auth/user/pass/MD5'
method: .get
}

mut d_auth := new_digest_authentification('user', 'pass', config) or { return }
mut rsp := d_auth.fetch() or { panic('$err') }
</code></pre>

</section>

<div class="footer">
Powered by vdoc. Generated on: 23 Mar 2022 14:27:30
Powered by vdoc. Generated on: 23 Mar 2022 15:36:53
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion v.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Module {
name: 'vigest_auth'
description: 'Digest authentification for v'
version: '0.0.4'
version: '0.0.5'
license: 'MIT'
dependencies: []
}

0 comments on commit f2e9def

Please sign in to comment.