Skip to content

Commit

Permalink
Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Apr 17, 2018
1 parent d31a108 commit 6dc748a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
19 changes: 0 additions & 19 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"math"
neturl "net/url"
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/agext/levenshtein"
)

type Config struct {
Expand Down Expand Up @@ -122,19 +119,3 @@ func (cfg *Config) Edit() error {
cmd.Stdin = os.Stdin
return cmd.Run()
}

func (cfg *Config) SimilarURLs(url string) (urls []string) {
u1, _ := neturl.Parse(url)
for _, service := range cfg.Services {
u2, _ := neturl.Parse(service.URL)
degree := round(levenshtein.Similarity(u1.Host, u2.Host, nil) * 100)
if degree > 50 {
urls = append(urls, service.URL)
}
}
return
}

func round(f float64) float64 {
return math.Floor(f + .5)
}
24 changes: 21 additions & 3 deletions iap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ import (
"golang.org/x/oauth2/jws"
)

type iapClient struct {
serviceAccount string
clientID string
}

func newIapClient(sa, clientID string) (*iapClient, error) {
if sa == "" {
return &iapClient{}, errors.New("service account is missing")
}
if clientID == "" {
return &iapClient{}, errors.New("client ID is missing")
}
return &iapClient{
serviceAccount: sa,
clientID: clientID,
}, nil
}

func readRsaPrivateKey(bytes []byte) (key *rsa.PrivateKey, err error) {
block, _ := pem.Decode(bytes)
if block == nil {
Expand Down Expand Up @@ -52,8 +70,8 @@ func readRsaPrivateKey(bytes []byte) (key *rsa.PrivateKey, err error) {
return
}

func getToken(saPath, clientID string) (token string, err error) {
sa, err := ioutil.ReadFile(saPath)
func (c *iapClient) GetToken() (token string, err error) {
sa, err := ioutil.ReadFile(c.serviceAccount)
if err != nil {
return
}
Expand All @@ -70,7 +88,7 @@ func getToken(saPath, clientID string) (token string, err error) {
Iat: iat.Unix(),
Exp: exp.Unix(),
PrivateClaims: map[string]interface{}{
"target_audience": clientID,
"target_audience": c.clientID,
},
}
jwsHeader := &jws.Header{
Expand Down
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

const (
// TokenURI is the base uri of google oauth API
TokenURI = "https://www.googleapis.com/oauth2/v4/token"

GoogleApplicationCredentials = "GOOGLE_APPLICATION_CREDENTIALS"
Expand Down Expand Up @@ -70,12 +71,7 @@ func run(args []string) int {
url := args[len(args)-1]
env, err := cfg.GetEnv(url)
if err != nil {
similarURLs := cfg.SimilarURLs(url)
if len(similarURLs) > 0 {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
fmt.Fprintf(os.Stderr, " similar urls found %q\n", similarURLs)
return 1
}
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
}
credentials = os.Getenv(GoogleApplicationCredentials)
clientID = os.Getenv(IAPClientID)
Expand All @@ -102,7 +98,12 @@ func run(args []string) int {
binary = "curl"
}

token, err := getToken(credentials, clientID)
proxy, err := newIapClient(credentials, clientID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
return 1
}
token, err := proxy.GetToken()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
return 1
Expand Down

0 comments on commit 6dc748a

Please sign in to comment.