Permalink
Browse files

Details

  • Loading branch information...
1 parent 02319b1 commit dad73b4943b599eaf86db157c3ff92d2f4c54ae0 @fduraffourg fduraffourg committed Aug 6, 2011
Showing with 51 additions and 50 deletions.
  1. +8 −12 README.markdown
  2. +3 −0 authrequest.go
  3. +8 −7 authrequest_test.go
  4. +2 −0 verify.go
  5. +9 −9 xrds.go
  6. +3 −3 xrds_test.go
  7. +13 −14 yadis.go
  8. +5 −5 yadis_test.go
View
@@ -18,25 +18,21 @@ Install
git clone http://github.com/fduraffourg/go-openid.git && cd go-openid && make && make install
+or
+ goinstall github.com/fduraffourg/go-openid
+
+
Usage
-----
url := openid.GetRedirectURL("Identifier", "http://www.realm.com", "/loginCheck")
Now you have to redirect the user to the url returned. The OP will then forward the user back to you, after authenticating him.
-** What follows has been removed from the code because it was not compliant with newer go code.**
-** Please wait a bit or use git history **
-
To check the identity, do that:
- var o = new(openid.OpenID)
- o.ParseRPUrl(URL)
- grant, err := o.Verify()
-
-grant is true if the user is authenticated, false otherwise. URL must contain the encoded content provided by the OP.
-
-Once o.ParseRPUrl(URL) is executed, all the information provided by the OP are in the map o.Params. For instance you get the identity with:
-
- o.Params["openid.claimed_id"]
+ grant, id, err := openid.Verify(URL)
+URL is the url the user was redirected to. grant will be true if the
+user was correctly authenticated, false otherwise. If the user was
+authenticated, id contains its identifier.
View
@@ -35,6 +35,9 @@ func GetRedirectURL(Identifier string, realm string, returnto string) (string, o
if err != nil {
return "", err
}
+ if reader == nil {
+ return "", os.ErrorString("Yadis returned an empty Reader for the ID: " + Id)
+ }
var endpoint, claimedid = ParseXRDS(reader)
if len(endpoint) == 0 {
View
@@ -13,10 +13,10 @@ import (
type NormalizeIdentifierTest struct {
in, out string
- t int
+ t int
}
-var NormalizeIdentifierTests = []NormalizeIdentifierTest {
+var NormalizeIdentifierTests = []NormalizeIdentifierTest{
//NormalizeIdentifierTest{"example.com", "http://example.com/", IdentifierURL},
//NormalizeIdentifierTest{"http://example.com", "http://example.com/", IdentifierURL},
NormalizeIdentifierTest{"https://example.com/", "https://example.com/", IdentifierURL},
@@ -30,25 +30,26 @@ var NormalizeIdentifierTests = []NormalizeIdentifierTest {
func TestNormalizeIdentifier(testing *testing.T) {
for _, nit := range NormalizeIdentifierTests {
v, t := NormalizeIdentifier(nit.in)
- if ! bytes.Equal([]byte(v), []byte(nit.out)) || t != nit.t {
+ if !bytes.Equal([]byte(v), []byte(nit.out)) || t != nit.t {
testing.Errorf("NormalizeIdentifier(%s) = (%s, %d) want (%s, %d).", nit.in, v, t, nit.out, nit.t)
}
}
}
// GetRedirectURL Test
-var Identifiers = []string {
+var Identifiers = []string{
"https://www.google.com/accounts/o8/id",
+ "orange.fr",
"yahoo.com",
}
// Just check that there is no errors returned by GetRedirectURL
-func TestGetRedirectURL ( t *testing.T) {
+func TestGetRedirectURL(t *testing.T) {
for _, url := range Identifiers {
- _,err := GetRedirectURL(url, "http://example.com", "/loginCheck")
+ _, err := GetRedirectURL(url, "http://example.com", "/loginCheck")
if err != nil {
t.Errorf("GetRedirectURL() returned the error: %s", err.String())
}
}
-}
+}
View
@@ -41,6 +41,8 @@ func Verify(url string) (grant bool, identifier string, err os.Error) {
return
}
+ identifier = urlm["openid.claimed_id"]
+
return
}
View
@@ -11,18 +11,18 @@ import (
)
type XRDSIdentifier struct {
- XMLName xml.Name "Service"
- Type []string
- URI string
- LocalID string
+ XMLName xml.Name "Service"
+ Type []string
+ URI string
+ LocalID string
}
type XRD struct {
XMLName xml.Name "XRD"
Service XRDSIdentifier
}
type XRDS struct {
XMLName xml.Name "XRDS"
- XRD XRD
+ XRD XRD
}
// Parse a XRDS document provided through a io.Reader
@@ -32,7 +32,7 @@ func ParseXRDS(r io.Reader) (string, string) {
err := xml.Unmarshal(r, XRDS)
if err != nil {
//fmt.Printf(err.String())
- return "", ""
+ return "", ""
}
XRDSI := XRDS.XRD.Service
@@ -41,7 +41,7 @@ func ParseXRDS(r io.Reader) (string, string) {
//fmt.Printf("%v\n", XRDSI)
- if StringTableContains(XRDSI.Type,"http://specs.openid.net/auth/2.0/server") {
+ if StringTableContains(XRDSI.Type, "http://specs.openid.net/auth/2.0/server") {
//fmt.Printf("OP Identifier Element found\n")
return XRDSI.URI, ""
} else if StringTableContains(XRDSI.Type, "http://specs.openid.net/auth/2.0/signon") {
@@ -52,8 +52,8 @@ func ParseXRDS(r io.Reader) (string, string) {
}
-func StringTableContains (t []string, s string) bool {
- for _,v := range t {
+func StringTableContains(t []string, s string) bool {
+ for _, v := range t {
if v == s {
return true
}
View
@@ -12,12 +12,12 @@ import (
// ParseXRDS Test
type ParseXRDSTest struct {
- in []byte
+ in []byte
OPEndPoint string
- ClaimedId string
+ ClaimedId string
}
-var ParseXRDSTests = []ParseXRDSTest {
+var ParseXRDSTests = []ParseXRDSTest{
ParseXRDSTest{[]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xrds:XRDS xmlns:xrds=\"xri://$xrds\" xmlns=\"xri://$xrd*($v*2.0)\"><XRD><Service xmlns=\"xri://$xrd*($v*2.0)\">\n<Type>http://specs.openid.net/auth/2.0/signon</Type>\n <URI>https://www.exampleprovider.com/endpoint/</URI>\n <LocalID>https://exampleuser.exampleprovider.com/</LocalID>\n </Service></XRD></xrds:XRDS>"), "https://www.exampleprovider.com/endpoint/", "https://exampleuser.exampleprovider.com/"},
ParseXRDSTest{[]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xrds:XRDS xmlns:xrds=\"xri://$xrds\" xmlns=\"xri://$xrd*($v*2.0)\">\n<XRD>\n <Service>\n <Type>http://specs.openid.net/auth/2.0/server</Type>\n <Type>http://openid.net/srv/ax/1.0</Type>\n <Type>http://openid.net/sreg/1.0</Type>\n <Type>http://openid.net/extensions/sreg/1.1</Type>\n <URI priority=\"20\">http://openid.orange.fr/server/</URI>\n </Service>\n</XRD>\n</xrds:XRDS>"), "http://openid.orange.fr/server/", ""},
}
View
@@ -17,7 +17,7 @@ import (
func Yadis(ID string) (io.Reader, os.Error) {
r, err := YadisRequest(ID, "GET")
- if (err != nil || r == nil) {
+ if err != nil || r == nil {
return nil, err
}
@@ -27,7 +27,7 @@ func Yadis(ID string) (io.Reader, os.Error) {
if strings.HasPrefix(contentType, "application/xrds+xml") {
return r.Body, nil
}
-
+
// If it is an HTML doc search for meta tags
if bytes.Equal([]byte(contentType), []byte("text/html")) {
url, err := searchHTMLMetaXRDS(r.Body)
@@ -36,7 +36,7 @@ func Yadis(ID string) (io.Reader, os.Error) {
}
return Yadis(url)
}
-
+
// If the response contain an X-XRDS-Location header
var xrds_location = r.Header.Get("X-Xrds-Location")
if len(xrds_location) > 0 {
@@ -47,7 +47,7 @@ func Yadis(ID string) (io.Reader, os.Error) {
return nil, nil
}
-func YadisRequest (url string, method string) (resp *http.Response, err os.Error) {
+func YadisRequest(url string, method string) (resp *http.Response, err os.Error) {
resp = nil
var request = new(http.Request)
@@ -56,20 +56,19 @@ func YadisRequest (url string, method string) (resp *http.Response, err os.Error
request.Method = method
request.RawURL = url
-
- request.URL , err = http.ParseURL(url)
+
+ request.URL, err = http.ParseURL(url)
if err != nil {
return
}
-
+
// Common parameters
request.Proto = "HTTP/1.0"
request.ProtoMajor = 1
request.ProtoMinor = 0
request.ContentLength = 0
request.Close = true
-
Header.Add("Accept", "application/xrds+xml")
request.Header = Header
@@ -80,7 +79,7 @@ func YadisRequest (url string, method string) (resp *http.Response, err os.Error
if response.StatusCode == 301 || response.StatusCode == 302 || response.StatusCode == 303 || response.StatusCode == 307 {
location := response.Header.Get("Location")
request.RawURL = location
- request.URL , err = http.ParseURL(location)
+ request.URL, err = http.ParseURL(location)
if err != nil {
return
}
@@ -96,10 +95,10 @@ func searchHTMLMetaXRDS(r io.Reader) (string, os.Error) {
var token xml.Token
var err os.Error
for {
- token, err = parser.Token();
- if (token == nil || err != nil) {
+ token, err = parser.Token()
+ if token == nil || err != nil {
if err == os.EOF {
- break;
+ break
}
return "", err
}
@@ -113,7 +112,7 @@ func searchHTMLMetaXRDS(r io.Reader) (string, os.Error) {
var httpEquivOK bool
contentE = false
httpEquivOK = false
- for _,v := range token.(xml.StartElement).Attr {
+ for _, v := range token.(xml.StartElement).Attr {
if v.Name.Local == "http-equiv" && v.Value == "X-XRDS-Location" {
httpEquivOK = true
}
@@ -128,5 +127,5 @@ func searchHTMLMetaXRDS(r io.Reader) (string, os.Error) {
}
}
}
- return "",os.ErrorString("Value not found")
+ return "", os.ErrorString("Value not found")
}
View
@@ -12,11 +12,11 @@ import (
// searchHTMLMetaXRDS Test
type searchHTMLMetaXRDSTest struct {
- in []byte
+ in []byte
out string
}
-var searchHTMLMetaXRDSTests = []searchHTMLMetaXRDSTest {
+var searchHTMLMetaXRDSTests = []searchHTMLMetaXRDSTest{
searchHTMLMetaXRDSTest{[]byte("<html><head><meta http-equiv='X-XRDS-Location' content='location'></meta></head></html>"), "location"},
//searchHTMLMetaXRDSTest{[]byte("<html><head><meta>location</meta></head></html>"), "location"},
}
@@ -27,7 +27,7 @@ func TestSearchHTMLMetaXRDS(t *testing.T) {
if err != nil {
t.Errorf("searchHTMLMetaXRDS error: %s", err.String())
}
- if ! bytes.Equal([]byte(content), []byte(l.out)) {
+ if !bytes.Equal([]byte(content), []byte(l.out)) {
t.Errorf("searchHTMLMetaXRDS(%s) = %s want %s.", l.in, content, l.out)
}
}
@@ -39,7 +39,7 @@ type YadisTest struct {
url string
}
-var YadisTests = []YadisTest {
+var YadisTests = []YadisTest{
YadisTest{"https://www.google.com/accounts/o8/id"},
YadisTest{"http://orange.fr/"},
YadisTest{"http://yahoo.com/"},
@@ -58,4 +58,4 @@ func TestYadis(t *testing.T) {
t.Errorf("Yadis(%s) returned a nil reader", yt.url)
}
}
-}
+}

0 comments on commit dad73b4

Please sign in to comment.