This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
126 lines (104 loc) · 3.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright © 2017 Shinichi MOTOKI
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"bufio"
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"github.com/shimt/go-simplecli"
"github.com/shimt/pam-exec-oauth2/internal/oauth2"
)
var cli = simplecli.NewCLI()
func initCLI() {
cli.CommandLine.String("client-id", "", "OAuth2 Client ID")
cli.CommandLine.String("client-secret", "", "OAuth2 Client Secret")
cli.CommandLine.StringArray("scopes", []string{}, "OAuth2 Scopes")
cli.CommandLine.String("redirect-url", "", "OAuth2 Redirect URL")
cli.CommandLine.String("endpoint-auth-url", "", "OAuth2 End Point Auth URL")
cli.CommandLine.String("endpoint-token-url", "", "OAuth2 End Point Token URL")
cli.CommandLine.String("username-format", "%s", "username format")
cli.BindSameName(
"client-id",
"client-secret",
"scopes",
"redirect-url",
"endpoint-auth-url",
"endpoint-token-url",
"username-format",
)
}
func init() {
initCLI()
}
func main() {
setting := cli.NewCLISetting()
err := cli.Setup(
setting.ConfigSearchPath(),
setting.ConfigFile(filepath.Join(cli.Application.Directory, cli.Application.Name+".yaml")),
)
cli.Exit1IfError(err)
if cli.ConfigFile != "" {
fmt.Println("Using config file:", cli.ConfigFile)
}
username := os.Getenv("PAM_USER")
password := ""
stdinScanner := bufio.NewScanner(os.Stdin)
if stdinScanner.Scan() {
password = stdinScanner.Text()
}
cli.Log.Debug("create oauth2Config")
cli.Log.Debugf("ClientID: %s", cli.Config.GetString("client-id"))
cli.Log.Debugf("ClientSecret: %s", cli.Config.GetString("client-secret"))
cli.Log.Debugf("Scopes: %s", cli.Config.GetStringSlice("scopes"))
cli.Log.Debugf("EndPoint.AuthURL: %s", cli.Config.GetString("endpoint-auth-url"))
cli.Log.Debugf("EndPoint.TokenURL: %s", cli.Config.GetString("endpoint-token-url"))
oauth2Config := oauth2.Config{
ClientID: cli.Config.GetString("client-id"),
ClientSecret: cli.Config.GetString("client-secret"),
Scopes: cli.Config.GetStringSlice("scopes"),
Endpoint: oauth2.Endpoint{
AuthURL: cli.Config.GetString("endpoint-auth-url"),
TokenURL: cli.Config.GetString("endpoint-token-url"),
},
}
extraParameters := url.Values{}
for k, v := range cli.Config.GetStringMapString("extra-parameters") {
extraParameters[k] = []string{v}
}
cli.Log.Debug("create oauth2Context")
oauth2Context := context.Background()
cli.Log.Debug("call PasswordCredentialsToken")
oauth2Token, err := oauth2Config.PasswordCredentialsTokenEx(
oauth2Context,
fmt.Sprintf(cli.Config.GetString("username-format"), username),
password,
extraParameters,
)
cli.Exit1IfError(err)
if !oauth2Token.Valid() {
cli.Exit(1)
cli.Log.Debug("OAuth2 authentication failed")
}
cli.Log.Debug("OAuth2 authentication success")
cli.Exit(0)
}