Skip to content

Commit

Permalink
Details
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Duraffourg committed Aug 6, 2011
1 parent 02319b1 commit dad73b4
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 50 deletions.
20 changes: 8 additions & 12 deletions README.markdown
Expand Up @@ -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.
3 changes: 3 additions & 0 deletions authrequest.go
Expand Up @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions authrequest_test.go
Expand Up @@ -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},
Expand All @@ -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())
}
}
}
}
2 changes: 2 additions & 0 deletions verify.go
Expand Up @@ -41,6 +41,8 @@ func Verify(url string) (grant bool, identifier string, err os.Error) {
return
}

identifier = urlm["openid.claimed_id"]

return
}

Expand Down
18 changes: 9 additions & 9 deletions xrds.go
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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") {
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions xrds_test.go
Expand Up @@ -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/", ""},
}
Expand Down
27 changes: 13 additions & 14 deletions yadis.go
Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -128,5 +127,5 @@ func searchHTMLMetaXRDS(r io.Reader) (string, os.Error) {
}
}
}
return "",os.ErrorString("Value not found")
return "", os.ErrorString("Value not found")
}
10 changes: 5 additions & 5 deletions yadis_test.go
Expand Up @@ -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"},
}
Expand All @@ -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)
}
}
Expand All @@ -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/"},
Expand All @@ -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.