| 1 | // |
| 2 | // Copyright (c) SAS Institute 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 | package token |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | |
| 24 | "github.com/sassoftware/relic/v7/config" |
| 25 | "github.com/sassoftware/relic/v7/lib/passprompt" |
| 26 | "github.com/sassoftware/relic/v7/signers/sigerrors" |
| 27 | ) |
| 28 | |
| 29 | func Login(tokenConf *config.TokenConfig, pinProvider passprompt.PasswordGetter, loginFunc passprompt.LoginFunc, keyringUser, initialPrompt string) error { |
| 30 | if tokenConf.Pin != nil { |
| 31 | ok, err := loginFunc(*tokenConf.Pin) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } else if !ok { |
| 35 | return sigerrors.PinIncorrectError{} |
| 36 | } else { |
| 37 | return nil |
| 38 | } |
| 39 | } |
| 40 | if initialPrompt == "" { |
| 41 | initialPrompt = fmt.Sprintf("PIN for token %s: ", tokenConf.Name()) |
| 42 | } |
| 43 | failPrefix := "Incorrect PIN\r\n" |
| 44 | var keyringService string |
| 45 | if tokenConf.UseKeyring { |
| 46 | keyringService = "relic" |
| 47 | } |
| 48 | err := passprompt.Login(loginFunc, pinProvider, keyringService, keyringUser, initialPrompt, failPrefix) |
| 49 | if err == io.EOF { |
| 50 | if pinProvider == nil { |
| 51 | msg := "PIN required but none was provided" |
| 52 | if tokenConf.UseKeyring { |
| 53 | msg += "; use 'relic ping' to save password in keyring" |
| 54 | } |
| 55 | return errors.New(msg) |
| 56 | } |
| 57 | return errors.New("Aborted") |
| 58 | } |
| 59 | return err |
| 60 | } |