| package handlers |
| |
| import ( |
| "encoding/base64" |
| "encoding/json" |
| "fmt" |
| "net/http" |
| |
| "v.io/core/veyron/services/identity/util" |
| "v.io/core/veyron2/security" |
| ) |
| |
| // BlessingRoot is an http.Handler implementation that renders the server's |
| // blessing names and public key in a json string. |
| type BlessingRoot struct { |
| P security.Principal |
| } |
| |
| type BlessingRootResponse struct { |
| // Names of the blessings. |
| Names []string `json:"names"` |
| // Base64 der-encoded public key. |
| PublicKey string `json:"publicKey"` |
| } |
| |
| // Cached response so we don't have to bless and encode every time somebody |
| // hits this route. |
| var cachedResponseJson []byte |
| |
| func (b BlessingRoot) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| if cachedResponseJson != nil { |
| respondJson(w, cachedResponseJson) |
| return |
| } |
| |
| // Get the blessing names of the local principal. |
| // TODO(nlacasse,ataly,gauthamt): Make this easier to do. For now we |
| // have to bless a context with the same LocalPrincipal as ours. |
| ctx := security.NewContext(&security.ContextParams{ |
| LocalPrincipal: b.P, |
| }) |
| names := b.P.BlessingStore().Default().ForContext(ctx) |
| |
| if len(names) == 0 { |
| util.HTTPServerError(w, fmt.Errorf("Could not get blessing name.")) |
| return |
| } |
| |
| // TODO(nlacasse,ashankar,ataly): The following line is a HACK. It |
| // marshals the public key of the *root* of the blessing chain, rather |
| // than the public key of the principal itself. |
| // |
| // We do this because the identity server is expected to be |
| // self-signed, and the javascript tests were breaking when the |
| // identity server is run with a blessing like test/child. |
| // |
| // Once this issue is resolved, delete the following line and uncomment |
| // the block below it. |
| der := security.MarshalBlessings(b.P.BlessingStore().Default()).CertificateChains[0][0].PublicKey |
| //der, err := b.P.PublicKey().MarshalBinary() |
| //if err != nil { |
| // util.HTTPServerError(w, err) |
| // return |
| //} |
| str := base64.URLEncoding.EncodeToString(der) |
| |
| res, err := json.Marshal(BlessingRootResponse{ |
| Names: names, |
| PublicKey: str, |
| }) |
| if err != nil { |
| util.HTTPServerError(w, err) |
| return |
| } |
| |
| cachedResponseJson = res |
| respondJson(w, res) |
| } |
| |
| func respondJson(w http.ResponseWriter, res []byte) { |
| w.Header().Set("Content-Type", "application/json") |
| w.Write(res) |
| } |