| 1 |
package openid2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"net/http" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
var ErrUnauthenticated = errors.New("authentication failed") |
| 11 |
|
| 12 |
|
| 13 |
type LoginRequest struct { |
| 14 |
ClaimedID string |
| 15 |
Identity string |
| 16 |
ReturnTo string |
| 17 |
Realm string |
| 18 |
Extensions []Extension |
| 19 |
} |
| 20 |
|
| 21 |
func parseLoginRequest(params map[string]string) (*LoginRequest, error) { |
| 22 |
extensions, err := parseExtensions(params) |
| 23 |
if err != nil { |
| 24 |
return nil, err |
| 25 |
} |
| 26 |
req := &LoginRequest{ |
| 27 |
ClaimedID: params["claimed_id"], |
| 28 |
Identity: params["identity"], |
| 29 |
ReturnTo: params["return_to"], |
| 30 |
Realm: params["realm"], |
| 31 |
Extensions: extensions, |
| 32 |
} |
| 33 |
return req, nil |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
type LoginResponse struct { |
| 38 |
ClaimedID string |
| 39 |
Identity string |
| 40 |
OPEndpoint string |
| 41 |
Extensions []Extension |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
type LoginHandler interface { |
| 46 |
Login(http.ResponseWriter, *http.Request, *LoginRequest) (*LoginResponse, error) |
| 47 |
} |
| 48 |
|
| 49 |
func (h *Handler) login(w http.ResponseWriter, r *http.Request, params map[string]string) { |
| 50 |
req, err := parseLoginRequest(params) |
| 51 |
if err != nil { |
| 52 |
indirect(w, params["return_to"]).respond(nil, err) |
| 53 |
return |
| 54 |
} |
| 55 |
var resp *LoginResponse |
| 56 |
switch params["mode"] { |
| 57 |
case "checkid_immediate": |
| 58 |
if h.Login != nil { |
| 59 |
resp, err = h.Login.Login(nil, r, req) |
| 60 |
} |
| 61 |
if err != nil && err != ErrUnauthenticated { |
| 62 |
indirect(w, params["return_to"]).respond(nil, err) |
| 63 |
return |
| 64 |
} |
| 65 |
if resp != nil { |
| 66 |
break |
| 67 |
} |
| 68 |
indirect(w, params["return_to"]).respond(map[string]string{ |
| 69 |
"ns": Namespace, |
| 70 |
"mode": "setup_needed", |
| 71 |
}, nil) |
| 72 |
return |
| 73 |
case "checkid_setup": |
| 74 |
if h.Login != nil { |
| 75 |
resp, err = h.Login.Login(w, r, req) |
| 76 |
} |
| 77 |
if err != nil && err != ErrUnauthenticated { |
| 78 |
indirect(w, params["return_to"]).respond(nil, err) |
| 79 |
return |
| 80 |
} |
| 81 |
if resp != nil { |
| 82 |
break |
| 83 |
} |
| 84 |
if err == nil { |
| 85 |
return |
| 86 |
} |
| 87 |
indirect(w, params["return_to"]).respond(map[string]string{ |
| 88 |
"ns": Namespace, |
| 89 |
"mode": "cancel", |
| 90 |
}, nil) |
| 91 |
return |
| 92 |
default: |
| 93 |
panic(fmt.Sprintf("login called with unexpected mode %q", params["mode"])) |
| 94 |
} |
| 95 |
if params["return_to"] == "" { |
| 96 |
direct(w).respond(nil, fmt.Errorf("cannot send id_res message, no return_to parameter")) |
| 97 |
return |
| 98 |
} |
| 99 |
nonce, err := h.getNonce() |
| 100 |
if err != nil { |
| 101 |
indirect(w, params["return_to"]).respond(nil, err) |
| 102 |
return |
| 103 |
} |
| 104 |
assoc, err := h.getAssociation(params["assoc_handle"], nonce) |
| 105 |
if err != nil { |
| 106 |
indirect(w, params["return_to"]).respond(nil, err) |
| 107 |
return |
| 108 |
} |
| 109 |
|
| 110 |
signed := []string{ |
| 111 |
"op_endpoint", |
| 112 |
"return_to", |
| 113 |
"response_nonce", |
| 114 |
"assoc_handle", |
| 115 |
} |
| 116 |
rparams := map[string]string{ |
| 117 |
"ns": Namespace, |
| 118 |
"mode": "id_res", |
| 119 |
"return_to": params["return_to"], |
| 120 |
"op_endpoint": resp.OPEndpoint, |
| 121 |
"response_nonce": nonce, |
| 122 |
"assoc_handle": assoc.Handle, |
| 123 |
} |
| 124 |
if resp.ClaimedID != "" { |
| 125 |
signed = append(signed, "claimed_id") |
| 126 |
rparams["claimed_id"] = resp.ClaimedID |
| 127 |
} |
| 128 |
if resp.Identity != "" { |
| 129 |
signed = append(signed, "identity") |
| 130 |
rparams["identity"] = resp.Identity |
| 131 |
} |
| 132 |
if params["assoc_handle"] != "" && params["assoc_handle"] != assoc.Handle { |
| 133 |
rparams["invalidate_handle"] = params["assoc_handle"] |
| 134 |
} |
| 135 |
signed = append(signed, encodeExtensions(rparams, resp.Extensions)...) |
| 136 |
rparams["signed"] = strings.Join(signed, ",") |
| 137 |
sig, err := assoc.sign(rparams, signed) |
| 138 |
if err != nil { |
| 139 |
indirect(w, params["return_to"]).respond(nil, err) |
| 140 |
return |
| 141 |
} |
| 142 |
rparams["sig"] = sig |
| 143 |
indirect(w, params["return_to"]).respond(rparams, nil) |
| 144 |
} |