Skip to content
Browse files

Separation of Yadis and modification of OpenID URL normalization

  • Loading branch information...
1 parent 48d97e5 commit 153773e31c007e40b9c0a70d40a433b9d93a019c @fduraffourg fduraffourg committed Aug 23, 2010
Showing with 65 additions and 19 deletions.
  1. +1 −0 Makefile
  2. +9 −19 openid.go
  3. +55 −0 yadis.go
View
1 Makefile
@@ -3,6 +3,7 @@ include $(GOROOT)/src/Make.$(GOARCH)
TARG=openid
GOFILES=\
openid.go\
+ yadis.go \
http.go
include $(GOROOT)/src/Make.pkg
View
28 openid.go
@@ -7,7 +7,6 @@ import (
"os"
"bytes"
"http"
-
)
@@ -22,28 +21,15 @@ type OpenID struct {
}
func (o *OpenID) normalizeIdentifier() {
+ match,_ := regexp.MatchString("https?://", o.Identifier)
+ if ! match {
+ o.Identifier = fmt.Sprintf("http://%s",o.Identifier)
+ }
+
return
}
-func Yadis(url string) string{
-
- r, err := get (url, nil)
- if (err != nil) { return "" }
-
- var buffer = make([]byte,1024)
- io.ReadFull(r.Body, buffer)
-
- URIRegex := regexp.MustCompile("<URI>.*</URI>")
- uris := URIRegex.MatchStrings(string(buffer))
- if len(uris) < 1 {
- return ""
- }
-
- uri := uris[0][5:len(uris[0])-6]
- fmt.Printf("%s\n",uri)
- return uri
-}
func mapToUrlEnc (params map[string] string) string {
url := ""
@@ -80,6 +66,9 @@ func (o *OpenID) GetUrl() string {
o.normalizeIdentifier()
URI := Yadis(o.Identifier)
+ if URI == "" {
+ return ""
+ }
params := map[string] string {
"openid.ns": "http://specs.openid.net/auth/2.0",
"openid.mode" : "checkid_setup",
@@ -130,6 +119,7 @@ func (o *OpenID) VerifyDirect() (grant bool, err os.Error) {
headers := map[string] string {
"Content-Type" : "application/x-www-form-urlencoded",
}
+ fmt.Printf("Verification: %s\n",o.Params["openid.op_endpoint"])
r,error := post(o.Params["openid.op_endpoint"],
headers,
bytes.NewBuffer([]byte(mapToUrlEnc(o.Params))))
View
55 yadis.go
@@ -0,0 +1,55 @@
+package openid
+
+import (
+ "io"
+ "regexp"
+ "fmt"
+ "strings"
+)
+
+func ParseXRDS(r io.Reader) string {
+ var buffer = make([]byte,1024)
+ io.ReadFull(r, buffer)
+ URIRegex := regexp.MustCompile("<URI[^>]*>.*</URI>")
+ uris := URIRegex.MatchStrings(string(buffer))
+ if len(uris) < 1 {
+ return ""
+ }
+ uri := uris[0]
+ start := strings.Index(uri,">") + 1
+ end := strings.Index(uri,"</")
+ uri = uri[start:end]
+ fmt.Printf("%s\n",uri)
+ return uri
+}
+
+func Yadis(url string) string{
+ fmt.Printf("Search: %s\n",url)
+ headers := map[string] string {
+ "Accept": "application/xrds+xml",
+ }
+ r, err := get (url, headers)
+ if (err != nil || r == nil) {
+ fmt.Printf("Error in GET\n")
+ return "" }
+
+ // If it is an XRDS document, parse it and return URI
+ content, ok := r.Header["Content-Type"]
+ if ok && content == "application/xrds+xml" {
+ fmt.Printf("Document XRDS found\n")
+ return ParseXRDS(r.Body)
+ }
+
+
+ // If it is an HTML doc search for meta tags
+
+ // If the response contain an X-XRDS-Location header
+ xrds, ok := r.Header["X-Xrds-Location"]
+ if ok {
+ return Yadis(xrds)
+ }
+
+ // If nothing is found try to parse it as a XRDS doc
+ return ParseXRDS(r.Body)
+
+}

0 comments on commit 153773e

Please sign in to comment.
Something went wrong with that request. Please try again.