github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/server/sigserver/sign.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"camlistore.org/pkg/httputil"
    21  	"camlistore.org/pkg/jsonsign"
    22  	"fmt"
    23  	"net/http"
    24  )
    25  
    26  const kMaxJsonLength = 1024 * 1024
    27  
    28  func handleSign(conn http.ResponseWriter, req *http.Request) {
    29  	if !(req.Method == "POST" && req.URL.Path == "/camli/sig/sign") {
    30  		httputil.BadRequestError(conn, "Inconfigured handler.")
    31  		return
    32  	}
    33  
    34  	req.ParseForm()
    35  
    36  	jsonStr := req.FormValue("json")
    37  	if jsonStr == "" {
    38  		httputil.BadRequestError(conn, "Missing json parameter")
    39  		return
    40  	}
    41  	if len(jsonStr) > kMaxJsonLength {
    42  		httputil.BadRequestError(conn, "json parameter too large")
    43  		return
    44  	}
    45  
    46  	sreq := &jsonsign.SignRequest{UnsignedJSON: jsonStr, Fetcher: pubKeyFetcher}
    47  	signedJson, err := sreq.Sign()
    48  	if err != nil {
    49  		// TODO: some aren't really a "bad request"
    50  		httputil.BadRequestError(conn, fmt.Sprintf("%v", err))
    51  		return
    52  	}
    53  	conn.Write([]byte(signedJson))
    54  }