Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Details

  • Loading branch information...
commit dad73b4943b599eaf86db157c3ff92d2f4c54ae0 1 parent 02319b1
Florian Duraffourg fduraffourg authored
20 README.markdown
Source Rendered
@@ -18,6 +18,10 @@ Install
18 18
19 19 git clone http://github.com/fduraffourg/go-openid.git && cd go-openid && make && make install
20 20
  21 +or
  22 + goinstall github.com/fduraffourg/go-openid
  23 +
  24 +
21 25 Usage
22 26 -----
23 27
@@ -25,18 +29,10 @@ Usage
25 29
26 30 Now you have to redirect the user to the url returned. The OP will then forward the user back to you, after authenticating him.
27 31
28   -** What follows has been removed from the code because it was not compliant with newer go code.**
29   -** Please wait a bit or use git history **
30   -
31 32 To check the identity, do that:
32 33
33   - var o = new(openid.OpenID)
34   - o.ParseRPUrl(URL)
35   - grant, err := o.Verify()
36   -
37   -grant is true if the user is authenticated, false otherwise. URL must contain the encoded content provided by the OP.
38   -
39   -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:
40   -
41   - o.Params["openid.claimed_id"]
  34 + grant, id, err := openid.Verify(URL)
42 35
  36 +URL is the url the user was redirected to. grant will be true if the
  37 +user was correctly authenticated, false otherwise. If the user was
  38 +authenticated, id contains its identifier.
3  authrequest.go
@@ -35,6 +35,9 @@ func GetRedirectURL(Identifier string, realm string, returnto string) (string, o
35 35 if err != nil {
36 36 return "", err
37 37 }
  38 + if reader == nil {
  39 + return "", os.ErrorString("Yadis returned an empty Reader for the ID: " + Id)
  40 + }
38 41
39 42 var endpoint, claimedid = ParseXRDS(reader)
40 43 if len(endpoint) == 0 {
15 authrequest_test.go
@@ -13,10 +13,10 @@ import (
13 13
14 14 type NormalizeIdentifierTest struct {
15 15 in, out string
16   - t int
  16 + t int
17 17 }
18 18
19   -var NormalizeIdentifierTests = []NormalizeIdentifierTest {
  19 +var NormalizeIdentifierTests = []NormalizeIdentifierTest{
20 20 //NormalizeIdentifierTest{"example.com", "http://example.com/", IdentifierURL},
21 21 //NormalizeIdentifierTest{"http://example.com", "http://example.com/", IdentifierURL},
22 22 NormalizeIdentifierTest{"https://example.com/", "https://example.com/", IdentifierURL},
@@ -30,7 +30,7 @@ var NormalizeIdentifierTests = []NormalizeIdentifierTest {
30 30 func TestNormalizeIdentifier(testing *testing.T) {
31 31 for _, nit := range NormalizeIdentifierTests {
32 32 v, t := NormalizeIdentifier(nit.in)
33   - if ! bytes.Equal([]byte(v), []byte(nit.out)) || t != nit.t {
  33 + if !bytes.Equal([]byte(v), []byte(nit.out)) || t != nit.t {
34 34 testing.Errorf("NormalizeIdentifier(%s) = (%s, %d) want (%s, %d).", nit.in, v, t, nit.out, nit.t)
35 35 }
36 36 }
@@ -38,17 +38,18 @@ func TestNormalizeIdentifier(testing *testing.T) {
38 38
39 39 // GetRedirectURL Test
40 40
41   -var Identifiers = []string {
  41 +var Identifiers = []string{
42 42 "https://www.google.com/accounts/o8/id",
  43 + "orange.fr",
43 44 "yahoo.com",
44 45 }
45 46
46 47 // Just check that there is no errors returned by GetRedirectURL
47   -func TestGetRedirectURL ( t *testing.T) {
  48 +func TestGetRedirectURL(t *testing.T) {
48 49 for _, url := range Identifiers {
49   - _,err := GetRedirectURL(url, "http://example.com", "/loginCheck")
  50 + _, err := GetRedirectURL(url, "http://example.com", "/loginCheck")
50 51 if err != nil {
51 52 t.Errorf("GetRedirectURL() returned the error: %s", err.String())
52 53 }
53 54 }
54   -}
  55 +}
2  verify.go
@@ -41,6 +41,8 @@ func Verify(url string) (grant bool, identifier string, err os.Error) {
41 41 return
42 42 }
43 43
  44 + identifier = urlm["openid.claimed_id"]
  45 +
44 46 return
45 47 }
46 48
18 xrds.go
@@ -11,10 +11,10 @@ import (
11 11 )
12 12
13 13 type XRDSIdentifier struct {
14   - XMLName xml.Name "Service"
15   - Type []string
16   - URI string
17   - LocalID string
  14 + XMLName xml.Name "Service"
  15 + Type []string
  16 + URI string
  17 + LocalID string
18 18 }
19 19 type XRD struct {
20 20 XMLName xml.Name "XRD"
@@ -22,7 +22,7 @@ type XRD struct {
22 22 }
23 23 type XRDS struct {
24 24 XMLName xml.Name "XRDS"
25   - XRD XRD
  25 + XRD XRD
26 26 }
27 27
28 28 // Parse a XRDS document provided through a io.Reader
@@ -32,7 +32,7 @@ func ParseXRDS(r io.Reader) (string, string) {
32 32 err := xml.Unmarshal(r, XRDS)
33 33 if err != nil {
34 34 //fmt.Printf(err.String())
35   - return "", ""
  35 + return "", ""
36 36 }
37 37 XRDSI := XRDS.XRD.Service
38 38
@@ -41,7 +41,7 @@ func ParseXRDS(r io.Reader) (string, string) {
41 41
42 42 //fmt.Printf("%v\n", XRDSI)
43 43
44   - if StringTableContains(XRDSI.Type,"http://specs.openid.net/auth/2.0/server") {
  44 + if StringTableContains(XRDSI.Type, "http://specs.openid.net/auth/2.0/server") {
45 45 //fmt.Printf("OP Identifier Element found\n")
46 46 return XRDSI.URI, ""
47 47 } else if StringTableContains(XRDSI.Type, "http://specs.openid.net/auth/2.0/signon") {
@@ -52,8 +52,8 @@ func ParseXRDS(r io.Reader) (string, string) {
52 52 }
53 53
54 54
55   -func StringTableContains (t []string, s string) bool {
56   - for _,v := range t {
  55 +func StringTableContains(t []string, s string) bool {
  56 + for _, v := range t {
57 57 if v == s {
58 58 return true
59 59 }
6 xrds_test.go
@@ -12,12 +12,12 @@ import (
12 12 // ParseXRDS Test
13 13
14 14 type ParseXRDSTest struct {
15   - in []byte
  15 + in []byte
16 16 OPEndPoint string
17   - ClaimedId string
  17 + ClaimedId string
18 18 }
19 19
20   -var ParseXRDSTests = []ParseXRDSTest {
  20 +var ParseXRDSTests = []ParseXRDSTest{
21 21 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/"},
22 22 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/", ""},
23 23 }
27 yadis.go
@@ -17,7 +17,7 @@ import (
17 17
18 18 func Yadis(ID string) (io.Reader, os.Error) {
19 19 r, err := YadisRequest(ID, "GET")
20   - if (err != nil || r == nil) {
  20 + if err != nil || r == nil {
21 21 return nil, err
22 22 }
23 23
@@ -27,7 +27,7 @@ func Yadis(ID string) (io.Reader, os.Error) {
27 27 if strings.HasPrefix(contentType, "application/xrds+xml") {
28 28 return r.Body, nil
29 29 }
30   -
  30 +
31 31 // If it is an HTML doc search for meta tags
32 32 if bytes.Equal([]byte(contentType), []byte("text/html")) {
33 33 url, err := searchHTMLMetaXRDS(r.Body)
@@ -36,7 +36,7 @@ func Yadis(ID string) (io.Reader, os.Error) {
36 36 }
37 37 return Yadis(url)
38 38 }
39   -
  39 +
40 40 // If the response contain an X-XRDS-Location header
41 41 var xrds_location = r.Header.Get("X-Xrds-Location")
42 42 if len(xrds_location) > 0 {
@@ -47,7 +47,7 @@ func Yadis(ID string) (io.Reader, os.Error) {
47 47 return nil, nil
48 48 }
49 49
50   -func YadisRequest (url string, method string) (resp *http.Response, err os.Error) {
  50 +func YadisRequest(url string, method string) (resp *http.Response, err os.Error) {
51 51 resp = nil
52 52
53 53 var request = new(http.Request)
@@ -56,12 +56,12 @@ func YadisRequest (url string, method string) (resp *http.Response, err os.Error
56 56
57 57 request.Method = method
58 58 request.RawURL = url
59   -
60   - request.URL , err = http.ParseURL(url)
  59 +
  60 + request.URL, err = http.ParseURL(url)
61 61 if err != nil {
62 62 return
63 63 }
64   -
  64 +
65 65 // Common parameters
66 66 request.Proto = "HTTP/1.0"
67 67 request.ProtoMajor = 1
@@ -69,7 +69,6 @@ func YadisRequest (url string, method string) (resp *http.Response, err os.Error
69 69 request.ContentLength = 0
70 70 request.Close = true
71 71
72   -
73 72 Header.Add("Accept", "application/xrds+xml")
74 73 request.Header = Header
75 74
@@ -80,7 +79,7 @@ func YadisRequest (url string, method string) (resp *http.Response, err os.Error
80 79 if response.StatusCode == 301 || response.StatusCode == 302 || response.StatusCode == 303 || response.StatusCode == 307 {
81 80 location := response.Header.Get("Location")
82 81 request.RawURL = location
83   - request.URL , err = http.ParseURL(location)
  82 + request.URL, err = http.ParseURL(location)
84 83 if err != nil {
85 84 return
86 85 }
@@ -96,10 +95,10 @@ func searchHTMLMetaXRDS(r io.Reader) (string, os.Error) {
96 95 var token xml.Token
97 96 var err os.Error
98 97 for {
99   - token, err = parser.Token();
100   - if (token == nil || err != nil) {
  98 + token, err = parser.Token()
  99 + if token == nil || err != nil {
101 100 if err == os.EOF {
102   - break;
  101 + break
103 102 }
104 103 return "", err
105 104 }
@@ -113,7 +112,7 @@ func searchHTMLMetaXRDS(r io.Reader) (string, os.Error) {
113 112 var httpEquivOK bool
114 113 contentE = false
115 114 httpEquivOK = false
116   - for _,v := range token.(xml.StartElement).Attr {
  115 + for _, v := range token.(xml.StartElement).Attr {
117 116 if v.Name.Local == "http-equiv" && v.Value == "X-XRDS-Location" {
118 117 httpEquivOK = true
119 118 }
@@ -128,5 +127,5 @@ func searchHTMLMetaXRDS(r io.Reader) (string, os.Error) {
128 127 }
129 128 }
130 129 }
131   - return "",os.ErrorString("Value not found")
  130 + return "", os.ErrorString("Value not found")
132 131 }
10 yadis_test.go
@@ -12,11 +12,11 @@ import (
12 12 // searchHTMLMetaXRDS Test
13 13
14 14 type searchHTMLMetaXRDSTest struct {
15   - in []byte
  15 + in []byte
16 16 out string
17 17 }
18 18
19   -var searchHTMLMetaXRDSTests = []searchHTMLMetaXRDSTest {
  19 +var searchHTMLMetaXRDSTests = []searchHTMLMetaXRDSTest{
20 20 searchHTMLMetaXRDSTest{[]byte("<html><head><meta http-equiv='X-XRDS-Location' content='location'></meta></head></html>"), "location"},
21 21 //searchHTMLMetaXRDSTest{[]byte("<html><head><meta>location</meta></head></html>"), "location"},
22 22 }
@@ -27,7 +27,7 @@ func TestSearchHTMLMetaXRDS(t *testing.T) {
27 27 if err != nil {
28 28 t.Errorf("searchHTMLMetaXRDS error: %s", err.String())
29 29 }
30   - if ! bytes.Equal([]byte(content), []byte(l.out)) {
  30 + if !bytes.Equal([]byte(content), []byte(l.out)) {
31 31 t.Errorf("searchHTMLMetaXRDS(%s) = %s want %s.", l.in, content, l.out)
32 32 }
33 33 }
@@ -39,7 +39,7 @@ type YadisTest struct {
39 39 url string
40 40 }
41 41
42   -var YadisTests = []YadisTest {
  42 +var YadisTests = []YadisTest{
43 43 YadisTest{"https://www.google.com/accounts/o8/id"},
44 44 YadisTest{"http://orange.fr/"},
45 45 YadisTest{"http://yahoo.com/"},
@@ -58,4 +58,4 @@ func TestYadis(t *testing.T) {
58 58 t.Errorf("Yadis(%s) returned a nil reader", yt.url)
59 59 }
60 60 }
61   -}
  61 +}

0 comments on commit dad73b4

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