github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/server/sigserver/camsigd.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  // The sigserver is a stand-alone JSON signing and verification server.
    18  //
    19  // TODO(bradfitz): as of 2012-01-10 this is very old and superceded by
    20  // the general server and pkg/serverconfig.  We should just make it
    21  // possible to configure a signing-only server with
    22  // serverconfig/genconfig.go.  I think we basically already can. Then
    23  // we can delete this.
    24  package main
    25  
    26  import (
    27  	"flag"
    28  	"fmt"
    29  	"log"
    30  	"net/http"
    31  
    32  	"camlistore.org/pkg/auth"
    33  	"camlistore.org/pkg/blob"
    34  	"camlistore.org/pkg/httputil"
    35  	"camlistore.org/pkg/webserver"
    36  )
    37  
    38  var accessPassword string
    39  
    40  var flagPubKeyDir = flag.String("pubkey-dir", "test/pubkey-blobs",
    41  	"Temporary development hack; directory to dig-xxxx.camli public keys.")
    42  
    43  // TODO: for now, the only implementation of the blobref.Fetcher
    44  // interface for fetching public keys is the "local, from disk"
    45  // implementation used for testing.  In reality we'd want to be able
    46  // to fetch these from blobservers.
    47  var pubKeyFetcher = blob.NewSimpleDirectoryFetcher(*flagPubKeyDir)
    48  
    49  func handleRoot(conn http.ResponseWriter, req *http.Request) {
    50  	fmt.Fprintf(conn, "camsigd")
    51  }
    52  
    53  func handleCamliSig(conn http.ResponseWriter, req *http.Request) {
    54  	handler := func(conn http.ResponseWriter, req *http.Request) {
    55  		httputil.BadRequestError(conn, "Unsupported path or method.")
    56  	}
    57  
    58  	switch req.Method {
    59  	case "POST":
    60  		switch req.URL.Path {
    61  		case "/camli/sig/sign":
    62  			handler = auth.RequireAuth(handleSign, auth.OpSign)
    63  		case "/camli/sig/verify":
    64  			handler = handleVerify
    65  		}
    66  	}
    67  	handler(conn, req)
    68  }
    69  
    70  func main() {
    71  	flag.Parse()
    72  
    73  	mode, err := auth.FromEnv()
    74  	if err != nil {
    75  		log.Fatal(err)
    76  	}
    77  	auth.SetMode(mode)
    78  
    79  	ws := webserver.New()
    80  	ws.HandleFunc("/", handleRoot)
    81  	ws.HandleFunc("/camli/sig/", handleCamliSig)
    82  	ws.Serve()
    83  }