Debian Codegraph @

auth/auth.go

raw symbols SCIP
1 //
2 // gosign - Go HTTP signing library for the Joyent Public Cloud and Joyent Manta
3 //
4 //
5 // Copyright (c) 2013 Joyent Inc.
6 //
7 // Written by Daniele Stroppa <[email protected]>
8 //
9
10 package auth
11
12 import (
13 "crypto"
14 "crypto/rand"
15 "crypto/rsa"
16 "crypto/x509"
17 "encoding/base64"
18 "encoding/pem"
19 "fmt"
20 "net/http"
21 "net/url"
22 "strings"
23 )
24
25 const (
26 // Authorization Headers
27 SdcSignature = "Signature keyId=\"/%s/keys/%s\",algorithm=\"%s\" %s"
28 MantaSignature = "Signature keyId=\"/%s/keys/%s\",algorithm=\"%s\",signature=\"%s\""
29 )
30
31 type Endpoint struct {
32 URL string
33 }
34
35 type Auth struct {
36 User string
37 PrivateKey PrivateKey
38 Algorithm string
39 }
40
41 type Credentials struct {
42 UserAuthentication *Auth
43 SdcKeyId string
44 SdcEndpoint Endpoint
45 MantaKeyId string
46 MantaEndpoint Endpoint
47 }
48
49 type PrivateKey struct {
50 key *rsa.PrivateKey
51 }
52
53 // NewAuth creates a new Auth.
54 func NewAuth(user, privateKey, algorithm string) (*Auth, error) {
55 block, _ := pem.Decode([]byte(privateKey))
56 if block == nil {
57 return nil, fmt.Errorf("invalid private key data: %s", privateKey)
58 }
59 rsakey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
60 if err != nil {
61 return nil, fmt.Errorf("An error occurred while parsing the key: %s", err)
62 }
63 return &Auth{user, PrivateKey{rsakey}, algorithm}, nil
64 }
65
66 // The CreateAuthorizationHeader returns the Authorization header for the give request.
67 func CreateAuthorizationHeader(headers http.Header, credentials *Credentials, isMantaRequest bool) (string, error) {
68 if isMantaRequest {
69 signature, err := GetSignature(credentials.UserAuthentication, "date: "+headers.Get("Date"))
70 if err != nil {
71 return "", err
72 }
73 return fmt.Sprintf(MantaSignature, credentials.UserAuthentication.User, credentials.MantaKeyId,
74 credentials.UserAuthentication.Algorithm, signature), nil
75 }
76 signature, err := GetSignature(credentials.UserAuthentication, headers.Get("Date"))
77 if err != nil {
78 return "", err
79 }
80 return fmt.Sprintf(SdcSignature, credentials.UserAuthentication.User, credentials.SdcKeyId,
81 credentials.UserAuthentication.Algorithm, signature), nil
82 }
83
84 // The GetSignature method signs the specified key according to http://apidocs.joyent.com/cloudapi/#issuing-requests
85 // and http://apidocs.joyent.com/manta/api.html#authentication.
86 func GetSignature(auth *Auth, signing string) (string, error) {
87 hashFunc := getHashFunction(auth.Algorithm)
88 hash := hashFunc.New()
89 hash.Write([]byte(signing))
90
91 digest := hash.Sum(nil)
92
93 signed, err := rsa.SignPKCS1v15(rand.Reader, auth.PrivateKey.key, hashFunc, digest)
94 if err != nil {
95 return "", fmt.Errorf("An error occurred while signing the key: %s", err)
96 }
97
98 return base64.StdEncoding.EncodeToString(signed), nil
99 }
100
101 // Helper method to get the Hash function based on the algorithm
102 func getHashFunction(algorithm string) (hashFunc crypto.Hash) {
103 switch strings.ToLower(algorithm) {
104 case "rsa-sha1":
105 hashFunc = crypto.SHA1
106 case "rsa-sha224", "rsa-sha256":
107 hashFunc = crypto.SHA256
108 case "rsa-sha384", "rsa-sha512":
109 hashFunc = crypto.SHA512
110 default:
111 hashFunc = crypto.SHA256
112 }
113 return
114 }
115
116 func (cred *Credentials) Region() string {
117 parsedUrl, err := url.Parse(cred.SdcEndpoint.URL)
118 if err != nil {
119 // Bogus URL - no region.
120 return ""
121 }
122 if strings.HasPrefix(parsedUrl.Host, "localhost") || strings.HasPrefix(parsedUrl.Host, "127.0.0.1") {
123 return "some-region"
124 }
125
126 host := parsedUrl.Host
127 firstDotIdx := strings.Index(host, ".")
128 if firstDotIdx >= 0 {
129 return host[:firstDotIdx]
130 }
131 return host
132 }