Debian Codegraph @

lib/passprompt/login.go

raw symbols SCIP
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 passprompt
18
19 import (
20 "fmt"
21 "io"
22 )
23
24 type LoginFunc func(string) (bool, error)
25
26 func Login(login LoginFunc, getter PasswordGetter, keyringService, keyringUser, initialPrompt, failPrefix string) error {
27 keyringFirst := keyringService != ""
28 prompt := initialPrompt
29 for {
30 var password string
31 var err error
32 if keyringFirst {
33 keyringFirst = false
34 password, err = keyringGet(keyringService, keyringUser)
35 if err == errNotFound {
36 continue
37 } else if err != nil {
38 return fmt.Errorf("keyring error: %w", err)
39 }
40 } else if getter != nil {
41 password, err = getter.GetPasswd(prompt)
42 if err != nil {
43 return err
44 } else if password == "" {
45 return io.EOF
46 }
47 } else {
48 return io.EOF
49 }
50 ok, err := login(password)
51 if err != nil {
52 return err
53 } else if ok {
54 if keyringService != "" {
55 err := keyringSet(keyringService, keyringUser, password)
56 if err != nil {
57 return fmt.Errorf("keyring error: %w", err)
58 }
59 }
60 return nil
61 }
62 prompt = failPrefix + initialPrompt
63 }
64 }