|
| 1 | +/* |
| 2 | + Copyright 2017 Cesanta Software Ltd. |
| 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 | + https://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 | +package authn |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + "cloud.google.com/go/storage" |
| 24 | + "github.com/cesanta/glog" |
| 25 | + "github.com/dchest/uniuri" |
| 26 | + "golang.org/x/crypto/bcrypt" |
| 27 | + "golang.org/x/net/context" |
| 28 | + "google.golang.org/api/option" |
| 29 | +) |
| 30 | + |
| 31 | +// NewGCSTokenDB return a new TokenDB structure which uses Google Cloud Storage as backend. The |
| 32 | +// created DB uses file-per-user strategy and stores credentials independently for each user. |
| 33 | +// |
| 34 | +// Note: it's not recomanded bucket to be shared with other apps or services |
| 35 | +func NewGCSTokenDB(bucket, clientSecretFile string) (TokenDB, error) { |
| 36 | + gcs, err := storage.NewClient(context.Background(), option.WithServiceAccountFile(clientSecretFile)) |
| 37 | + return &gcsTokenDB{gcs, bucket}, err |
| 38 | +} |
| 39 | + |
| 40 | +type gcsTokenDB struct { |
| 41 | + gcs *storage.Client |
| 42 | + bucket string |
| 43 | +} |
| 44 | + |
| 45 | +// GetValue gets token value associated with the provided user. Each user |
| 46 | +// in the bucket is having it's own file for tokens and it's recomanded bucket |
| 47 | +// to not be shared with other apps |
| 48 | +func (db *gcsTokenDB) GetValue(user string) (*TokenDBValue, error) { |
| 49 | + rd, err := db.gcs.Bucket(db.bucket).Object(user).NewReader(context.Background()) |
| 50 | + if err == storage.ErrObjectNotExist { |
| 51 | + return nil, nil |
| 52 | + } |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("could not retrieved token for user '%s' due: %v", user, err) |
| 55 | + } |
| 56 | + defer rd.Close() |
| 57 | + |
| 58 | + var dbv TokenDBValue |
| 59 | + if err := json.NewDecoder(rd).Decode(&dbv); err != nil { |
| 60 | + glog.Errorf("bad DB value for %q: %v", user, err) |
| 61 | + return nil, fmt.Errorf("could not read token for user '%s' due: %v", user, err) |
| 62 | + } |
| 63 | + |
| 64 | + return &dbv, nil |
| 65 | +} |
| 66 | + |
| 67 | +// StoreToken stores token in the GCS file in a JSON format. Note that separate file is |
| 68 | +// used for each user |
| 69 | +func (db *gcsTokenDB) StoreToken(user string, v *TokenDBValue, updatePassword bool) (dp string, err error) { |
| 70 | + if updatePassword { |
| 71 | + dp = uniuri.New() |
| 72 | + dph, _ := bcrypt.GenerateFromPassword([]byte(dp), bcrypt.DefaultCost) |
| 73 | + v.DockerPassword = string(dph) |
| 74 | + } |
| 75 | + |
| 76 | + wr := db.gcs.Bucket(db.bucket).Object(user).NewWriter(context.Background()) |
| 77 | + |
| 78 | + if err := json.NewEncoder(wr).Encode(v); err != nil { |
| 79 | + glog.Errorf("failed to set token data for %s: %s", user, err) |
| 80 | + return "", fmt.Errorf("failed to set token data for %s due: %v", user, err) |
| 81 | + } |
| 82 | + |
| 83 | + err = wr.Close() |
| 84 | + return |
| 85 | +} |
| 86 | + |
| 87 | +// ValidateToken verifies whether the provided token passed as password field |
| 88 | +// is still valid, e.g available and not expired |
| 89 | +func (db *gcsTokenDB) ValidateToken(user string, password PasswordString) error { |
| 90 | + dbv, err := db.GetValue(user) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + if dbv == nil { |
| 95 | + return NoMatch |
| 96 | + } |
| 97 | + |
| 98 | + if bcrypt.CompareHashAndPassword([]byte(dbv.DockerPassword), []byte(password)) != nil { |
| 99 | + return WrongPass |
| 100 | + } |
| 101 | + if time.Now().After(dbv.ValidUntil) { |
| 102 | + return ExpiredToken |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +// DeleteToken deletes the GCS file that is associated with the provided user. |
| 109 | +func (db *gcsTokenDB) DeleteToken(user string) error { |
| 110 | + ctx := context.Background() |
| 111 | + err := db.gcs.Bucket(db.bucket).Object(user).Delete(ctx) |
| 112 | + if err == storage.ErrObjectNotExist { |
| 113 | + return nil |
| 114 | + } |
| 115 | + return err |
| 116 | +} |
| 117 | + |
| 118 | +// Close is a nop operation for this db |
| 119 | +func (db *gcsTokenDB) Close() error { |
| 120 | + return nil |
| 121 | +} |
0 commit comments