| 1 | package options |
| 2 | |
| 3 | const ( |
| 4 | RSAKey = "rsa" |
| 5 | Ed25519Key = "ed25519" |
| 6 | |
| 7 | DefaultRSALen = 2048 |
| 8 | ) |
| 9 | |
| 10 | type KeyGenerateSettings struct { |
| 11 | Algorithm string |
| 12 | Size int |
| 13 | } |
| 14 | |
| 15 | type KeyRenameSettings struct { |
| 16 | Force bool |
| 17 | } |
| 18 | |
| 19 | type ( |
| 20 | KeyGenerateOption func(*KeyGenerateSettings) error |
| 21 | KeyRenameOption func(*KeyRenameSettings) error |
| 22 | ) |
| 23 | |
| 24 | func KeyGenerateOptions(opts ...KeyGenerateOption) (*KeyGenerateSettings, error) { |
| 25 | options := &KeyGenerateSettings{ |
| 26 | Algorithm: RSAKey, |
| 27 | Size: -1, |
| 28 | } |
| 29 | |
| 30 | for _, opt := range opts { |
| 31 | err := opt(options) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | } |
| 36 | return options, nil |
| 37 | } |
| 38 | |
| 39 | func KeyRenameOptions(opts ...KeyRenameOption) (*KeyRenameSettings, error) { |
| 40 | options := &KeyRenameSettings{ |
| 41 | Force: false, |
| 42 | } |
| 43 | |
| 44 | for _, opt := range opts { |
| 45 | err := opt(options) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | } |
| 50 | return options, nil |
| 51 | } |
| 52 | |
| 53 | type keyOpts struct{} |
| 54 | |
| 55 | var Key keyOpts |
| 56 | |
| 57 | // Type is an option for Key.Generate which specifies which algorithm |
| 58 | // should be used for the key. Default is options.RSAKey |
| 59 | // |
| 60 | // Supported key types: |
| 61 | // * options.RSAKey |
| 62 | // * options.Ed25519Key |
| 63 | func (keyOpts) Type(algorithm string) KeyGenerateOption { |
| 64 | return func(settings *KeyGenerateSettings) error { |
| 65 | settings.Algorithm = algorithm |
| 66 | return nil |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Size is an option for Key.Generate which specifies the size of the key to |
| 71 | // generated. Default is -1 |
| 72 | // |
| 73 | // value of -1 means 'use default size for key type': |
| 74 | // - 2048 for RSA |
| 75 | func (keyOpts) Size(size int) KeyGenerateOption { |
| 76 | return func(settings *KeyGenerateSettings) error { |
| 77 | settings.Size = size |
| 78 | return nil |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Force is an option for Key.Rename which specifies whether to allow to |
| 83 | // replace existing keys. |
| 84 | func (keyOpts) Force(force bool) KeyRenameOption { |
| 85 | return func(settings *KeyRenameSettings) error { |
| 86 | settings.Force = force |
| 87 | return nil |
| 88 | } |
| 89 | } |