|
| 1 | +package localxpose |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/99designs/keyring" |
| 7 | + |
| 8 | + "github.com/1Password/shell-plugins/sdk" |
| 9 | + "github.com/1Password/shell-plugins/sdk/importer" |
| 10 | + "github.com/1Password/shell-plugins/sdk/provision" |
| 11 | + "github.com/1Password/shell-plugins/sdk/schema" |
| 12 | + "github.com/1Password/shell-plugins/sdk/schema/credname" |
| 13 | + "github.com/1Password/shell-plugins/sdk/schema/fieldname" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + serviceName = "https://loclx.io" |
| 18 | + keyringKeyName = "token" |
| 19 | + tokenFilePath = "~/.localxpose/.access" |
| 20 | +) |
| 21 | + |
| 22 | +func AccessToken() schema.CredentialType { |
| 23 | + return schema.CredentialType{ |
| 24 | + Name: credname.AccessToken, |
| 25 | + DocsURL: sdk.URL("https://localxpose.io/docs"), |
| 26 | + ManagementURL: sdk.URL("https://localxpose.io/dashboard/access"), |
| 27 | + Fields: []schema.CredentialField{ |
| 28 | + { |
| 29 | + Name: fieldname.AccessToken, |
| 30 | + MarkdownDescription: "Token used to authenticate to LocalXpose.", |
| 31 | + Secret: true, |
| 32 | + Composition: &schema.ValueComposition{ |
| 33 | + Length: 40, |
| 34 | + Charset: schema.Charset{ |
| 35 | + Uppercase: true, |
| 36 | + Lowercase: true, |
| 37 | + Digits: true, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), |
| 43 | + Importer: importer.TryAll( |
| 44 | + importer.TryEnvVarPair(defaultEnvVarMapping), |
| 45 | + TryOSKeyring(), |
| 46 | + TryAccessTokenFile(), |
| 47 | + )} |
| 48 | +} |
| 49 | + |
| 50 | +var defaultEnvVarMapping = map[string]sdk.FieldName{ |
| 51 | + "LX_ACCESS_TOKEN": fieldname.AccessToken, |
| 52 | +} |
| 53 | + |
| 54 | +func TryAccessTokenFile() sdk.Importer { |
| 55 | + return importer.TryFile(tokenFilePath, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { |
| 56 | + if contents.ToString() == "" { |
| 57 | + return |
| 58 | + } |
| 59 | + out.AddCandidate(sdk.ImportCandidate{ |
| 60 | + Fields: map[sdk.FieldName]string{ |
| 61 | + fieldname.AccessToken: contents.ToString(), |
| 62 | + }, |
| 63 | + }) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +func TryOSKeyring() sdk.Importer { |
| 68 | + return func(ctx context.Context, in sdk.ImportInput, out *sdk.ImportOutput) { |
| 69 | + availableBackends := keyring.AvailableBackends() |
| 70 | + if len(availableBackends) == 0 { |
| 71 | + return |
| 72 | + } |
| 73 | + for _, backendType := range availableBackends { |
| 74 | + attempt := out.NewAttempt(importer.SourceOther(string(backendType), "")) |
| 75 | + openKeyring, err := keyring.Open(keyring.Config{ |
| 76 | + KeychainTrustApplication: true, |
| 77 | + ServiceName: serviceName, |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + attempt.AddError(err) |
| 81 | + return |
| 82 | + } |
| 83 | + key, err := openKeyring.Get(keyringKeyName) |
| 84 | + if err != nil { |
| 85 | + attempt.AddError(err) |
| 86 | + continue |
| 87 | + } |
| 88 | + attempt.AddCandidate(sdk.ImportCandidate{ |
| 89 | + Fields: map[sdk.FieldName]string{ |
| 90 | + fieldname.AccessToken: string(key.Data), |
| 91 | + }, |
| 92 | + }) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments