本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.9KB

  1. package openid
  2. import (
  3. "net/url"
  4. "strings"
  5. )
  6. func RedirectURL(id, callbackURL, realm string) (string, error) {
  7. return defaultInstance.RedirectURL(id, callbackURL, realm)
  8. }
  9. func (oid *OpenID) RedirectURL(id, callbackURL, realm string) (string, error) {
  10. opEndpoint, opLocalID, claimedID, err := oid.Discover(id)
  11. if err != nil {
  12. return "", err
  13. }
  14. return BuildRedirectURL(opEndpoint, opLocalID, claimedID, callbackURL, realm)
  15. }
  16. func BuildRedirectURL(opEndpoint, opLocalID, claimedID, returnTo, realm string) (string, error) {
  17. values := make(url.Values)
  18. values.Add("openid.ns", "http://specs.openid.net/auth/2.0")
  19. values.Add("openid.mode", "checkid_setup")
  20. values.Add("openid.return_to", returnTo)
  21. // 9.1. Request Parameters
  22. // "openid.claimed_id" and "openid.identity" SHALL be either both present or both absent.
  23. if len(claimedID) > 0 {
  24. values.Add("openid.claimed_id", claimedID)
  25. if len(opLocalID) > 0 {
  26. values.Add("openid.identity", opLocalID)
  27. } else {
  28. // If a different OP-Local Identifier is not specified,
  29. // the claimed identifier MUST be used as the value for openid.identity.
  30. values.Add("openid.identity", claimedID)
  31. }
  32. } else {
  33. // 7.3.1. Discovered Information
  34. // If the end user entered an OP Identifier, there is no Claimed Identifier.
  35. // For the purposes of making OpenID Authentication requests, the value
  36. // "http://specs.openid.net/auth/2.0/identifier_select" MUST be used as both the
  37. // Claimed Identifier and the OP-Local Identifier when an OP Identifier is entered.
  38. values.Add("openid.claimed_id", "http://specs.openid.net/auth/2.0/identifier_select")
  39. values.Add("openid.identity", "http://specs.openid.net/auth/2.0/identifier_select")
  40. }
  41. if len(realm) > 0 {
  42. values.Add("openid.realm", realm)
  43. }
  44. if strings.Contains(opEndpoint, "?") {
  45. return opEndpoint + "&" + values.Encode(), nil
  46. }
  47. return opEndpoint + "?" + values.Encode(), nil
  48. }
上海开阖软件有限公司 沪ICP备12045867号-1