package digest
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/icholy/digest/internal/param"
)
type Credentials struct {
Username string
Realm string
Nonce string
URI string
Response string
Algorithm string
Cnonce string
Opaque string
QOP string
Nc int
Userhash bool
}
func ParseCredentials (s string ) (*Credentials , error ) {
s , ok := CutPrefix (s )
if !ok {
return nil , errors .New ("digest: invalid credentials prefix" )
}
pp , err := param .Parse (s )
if err != nil {
return nil , fmt .Errorf ("digest: invalid credentials: %w" , err )
}
var c Credentials
for _ , p := range pp {
switch p .Key {
case "username" :
c .Username = p .Value
case "realm" :
c .Realm = p .Value
case "nonce" :
c .Nonce = p .Value
case "uri" :
c .URI = p .Value
case "response" :
c .Response = p .Value
case "algorithm" :
c .Algorithm = p .Value
case "cnonce" :
c .Cnonce = p .Value
case "opaque" :
c .Opaque = p .Value
case "qop" :
c .QOP = p .Value
case "nc" :
nc , err := strconv .ParseInt (p .Value , 16 , 32 )
if err != nil {
return nil , fmt .Errorf ("digest: invalid nc: %w" , err )
}
c .Nc = int (nc )
case "userhash" :
c .Userhash = strings .ToLower (p .Value ) == "true"
}
}
return &c , nil
}
func (c *Credentials ) String () string {
var pp []param .Param
pp = append (pp ,
param .Param {
Key : "username" ,
Value : c .Username ,
Quote : true ,
},
param .Param {
Key : "realm" ,
Value : c .Realm ,
Quote : true ,
},
param .Param {
Key : "nonce" ,
Value : c .Nonce ,
Quote : true ,
},
param .Param {
Key : "uri" ,
Value : c .URI ,
Quote : true ,
},
)
if c .Algorithm != "" {
pp = append (pp , param .Param {
Key : "algorithm" ,
Value : c .Algorithm ,
})
}
if c .QOP != "" {
pp = append (pp , param .Param {
Key : "cnonce" ,
Value : c .Cnonce ,
Quote : true ,
})
}
if c .Opaque != "" {
pp = append (pp , param .Param {
Key : "opaque" ,
Value : c .Opaque ,
Quote : true ,
})
}
if c .QOP != "" {
pp = append (pp ,
param .Param {
Key : "qop" ,
Value : c .QOP ,
},
param .Param {
Key : "nc" ,
Value : fmt .Sprintf ("%08x" , c .Nc ),
},
)
}
if c .Userhash {
pp = append (pp , param .Param {
Key : "userhash" ,
Value : "true" ,
})
}
pp = append (pp , param .Param {
Key : "response" ,
Value : c .Response ,
Quote : true ,
})
return Prefix + param .Format (pp ...)
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .