auth

Paddy 2014-10-26 Parent:a5987795707e Child:b3cd7765a7c8

57:e45bfa2abc00 Go to Latest

auth/http.go

The great documentation and exported interface cleanup. Modify all our *Store interfaces to be unexported, as there's no real good reason they need to be exported, especially as they can be implemented without being exported. The interfaces shouldn't matter to 99% of users of the package, so let's not pollute our package API. Further, all methods of the interfaces are now unexported, for pretty much the same reasoning. Add a doc.go file with documentation explaining the choices the package is making and what it provides. Implement documentation on all our exported types and methods and functions, which makes golint happy. The only remaining golint warning is about NewMemstore, which will stay the way it is. The memstore type is useful outside tests for things like standing up a server quickly when we don't care about the storage, and because the type is unexported, we _need_ a New function to create an instance that can be passed to the Context.

History
1 package auth
3 import (
4 "net/http"
6 "code.secondbit.org/uuid"
7 )
9 const getGrantTemplateName = "get_grant"
11 // GetGrantHandler presents and processes the page for asking a user to grant access
12 // to their data. See RFC 6749, Section 4.1.
13 func GetGrantHandler(w http.ResponseWriter, r *http.Request, context Context) {
14 if r.URL.Query().Get("client_id") == "" {
15 w.WriteHeader(http.StatusBadRequest)
16 context.Render(w, getGrantTemplateName, map[string]interface{}{
17 "error": "Client ID must be specified in the request.",
18 })
19 return
20 }
21 clientID, err := uuid.Parse(r.URL.Query().Get("client_id"))
22 if err != nil {
23 w.WriteHeader(http.StatusBadRequest)
24 context.Render(w, getGrantTemplateName, map[string]interface{}{
25 "error": "client_id is not a valid Client ID.",
26 })
27 return
28 }
29 client, err := context.GetClient(clientID)
30 if err != nil {
31 w.WriteHeader(http.StatusInternalServerError)
32 context.Render(w, getGrantTemplateName, map[string]interface{}{
33 "internal_error": err,
34 })
35 return
36 }
37 // whether a redirect URI is valid or not depends on the number of endpoints
38 // the client has registered
39 numEndpoints, err := context.CountEndpoints(clientID)
40 if err != nil {
41 w.WriteHeader(http.StatusInternalServerError)
42 context.Render(w, getGrantTemplateName, map[string]interface{}{
43 "internal_error": err,
44 })
45 return
46 }
47 redirectURI := r.URL.Query().Get("redirect_uri")
48 var validURI bool
49 if redirectURI != "" && numEndpoints > 1 {
50 // if there's more than one registered endpoint, we need to match the
51 // entire thing, character for character. So use strict checking.
52 validURI, err = context.CheckEndpoint(clientID, redirectURI, true)
53 if err != nil {
54 w.WriteHeader(http.StatusInternalServerError)
55 context.Render(w, getGrantTemplateName, map[string]interface{}{
56 "internal_error": err,
57 })
58 return
59 }
60 } else if redirectURI != "" && numEndpoints == 1 {
61 // if there's exactly one endpoint, we can match only the prefix of it,
62 // so don't use strict checking.
63 validURI, err = context.CheckEndpoint(clientID, redirectURI, false)
64 if err != nil {
65 w.WriteHeader(http.StatusInternalServerError)
66 context.Render(w, getGrantTemplateName, map[string]interface{}{
67 "internal_error": err,
68 })
69 return
70 }
71 } else if redirectURI == "" && numEndpoints == 1 {
72 // if we don't specify the endpoint and there's only one endpoint, the
73 // request is valid, and we're redirecting to that one endpoint
74 validURI = true
75 endpoints, err := context.ListEndpoints(clientID, 1, 0)
76 if err != nil {
77 w.WriteHeader(http.StatusInternalServerError)
78 context.Render(w, getGrantTemplateName, map[string]interface{}{
79 "internal_error": err,
80 })
81 return
82 }
83 if len(endpoints) != 1 {
84 validURI = false
85 } else {
86 redirectURI = endpoints[0].URI.String()
87 }
88 } else {
89 validURI = false
90 }
91 if !validURI {
92 w.WriteHeader(http.StatusBadRequest)
93 context.Render(w, getGrantTemplateName, map[string]interface{}{
94 "error": "The redirect_uri specified is not valid.",
95 })
96 return
97 }
98 if r.URL.Query().Get("response_type") != "code" {
99 // TODO: redirect error
100 }
101 //scope := r.URL.Query().Get("scope")
102 //state := r.URL.Query().Get("state")
103 if r.Method == "POST" {
104 // TODO: CSRF protection
105 if r.PostFormValue("grant") == "approved" {
106 // TODO: redirect
107 } else {
108 // TODO: redirect error
109 }
110 }
111 w.WriteHeader(http.StatusOK)
112 context.Render(w, getGrantTemplateName, map[string]interface{}{
113 "client": client,
114 })
115 }