Skip to content

Commit 0d7fe2c

Browse files
committed
fix project compilation
1 parent 6eca257 commit 0d7fe2c

File tree

7 files changed

+356
-63
lines changed

7 files changed

+356
-63
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildNodeFrontend:
99
cd web && rm build/static/**/*.map
1010

1111
embedFrontend:
12-
cd cmd/golang-url-shortener && packr2
12+
cd cmd/golang-url-shortener && packr2 --legacy
1313

1414
getCMDDependencies:
1515
go get -v github.com/mattn/goveralls

go.mod

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module github.com/mxschmitt/golang-url-shortener
2+
3+
go 1.15
4+
5+
require (
6+
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef
7+
github.com/boltdb/bolt v1.3.1
8+
github.com/dgrijalva/jwt-go v3.2.0+incompatible
9+
github.com/gin-contrib/sessions v0.0.3
10+
github.com/gin-gonic/gin v1.6.3
11+
github.com/go-redis/redis v6.15.9+incompatible
12+
github.com/gobuffalo/packr/v2 v2.8.1
13+
github.com/mattn/goveralls v0.0.7 // indirect
14+
github.com/mitchellh/gox v1.0.1 // indirect
15+
github.com/mxschmitt/golang-env-struct v0.0.0-20181017075525-0c54aeca8397
16+
github.com/pborman/uuid v1.2.1
17+
github.com/pkg/errors v0.8.0
18+
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18
19+
github.com/sirupsen/logrus v1.4.2
20+
golang.org/x/crypto v0.0.0-20191122220453-ac88ee75c92c
21+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
22+
gopkg.in/yaml.v2 v2.2.8
23+
)

go.sum

Lines changed: 268 additions & 0 deletions
Large diffs are not rendered by default.

internal/handlers/auth/github.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package auth
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67

78
"github.com/mxschmitt/golang-url-shortener/internal/util"
89
"github.com/sirupsen/logrus"
@@ -63,7 +64,7 @@ func (a *githubAdapter) GetUserData(state, code string) (*user, error) {
6364
return nil, errors.Wrap(err, "decoding user info failed")
6465
}
6566
return &user{
66-
ID: string(gUser.ID),
67+
ID: fmt.Sprint(gUser.ID),
6768
Name: gUser.Name,
6869
Picture: gUser.AvatarURL + "&s=64",
6970
}, nil

internal/handlers/auth/okta.go

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,85 @@
11
package auth
22

33
import (
4-
"context"
5-
"encoding/json"
6-
"net/url"
7-
"strings"
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"net/url"
8+
"strings"
89

9-
"github.com/mxschmitt/golang-url-shortener/internal/util"
10-
"github.com/sirupsen/logrus"
10+
"github.com/mxschmitt/golang-url-shortener/internal/util"
11+
"github.com/sirupsen/logrus"
1112

12-
"github.com/pkg/errors"
13-
"golang.org/x/oauth2"
13+
"github.com/pkg/errors"
14+
"golang.org/x/oauth2"
1415
)
1516

1617
type oktaAdapter struct {
17-
config *oauth2.Config
18+
config *oauth2.Config
1819
}
1920

2021
// NewOktaAdapter creates an oAuth adapter out of the credentials and the baseURL
2122
func NewOktaAdapter(clientID, clientSecret, endpointURL string) Adapter {
2223

23-
if endpointURL == "" {
24-
logrus.Error("Configure Okta Endpoint")
25-
}
24+
if endpointURL == "" {
25+
logrus.Error("Configure Okta Endpoint")
26+
}
2627

27-
return &oktaAdapter{&oauth2.Config{
28-
ClientID: clientID,
29-
ClientSecret: clientSecret,
30-
RedirectURL: util.GetConfig().BaseURL + "/api/v1/auth/okta/callback",
31-
Scopes: []string{
32-
"profile",
33-
"openid",
34-
"offline_access",
35-
},
36-
Endpoint: oauth2.Endpoint{
37-
AuthURL: endpointURL + "/v1/authorize",
38-
TokenURL: endpointURL + "/v1/token",
39-
},
40-
}}
28+
return &oktaAdapter{&oauth2.Config{
29+
ClientID: clientID,
30+
ClientSecret: clientSecret,
31+
RedirectURL: util.GetConfig().BaseURL + "/api/v1/auth/okta/callback",
32+
Scopes: []string{
33+
"profile",
34+
"openid",
35+
"offline_access",
36+
},
37+
Endpoint: oauth2.Endpoint{
38+
AuthURL: endpointURL + "/v1/authorize",
39+
TokenURL: endpointURL + "/v1/token",
40+
},
41+
}}
4142
}
4243

4344
func (a *oktaAdapter) GetRedirectURL(state string) string {
44-
return a.config.AuthCodeURL(state)
45+
return a.config.AuthCodeURL(state)
4546
}
4647

4748
func (a *oktaAdapter) GetUserData(state, code string) (*user, error) {
4849

49-
logrus.Debugf("Getting User Data with state: %s, and code: %s", state, code)
50-
oAuthToken, err := a.config.Exchange(context.Background(), code)
51-
if err != nil {
52-
return nil, errors.Wrap(err, "could not exchange code")
53-
}
54-
if util.GetConfig().Okta.EndpointURL == "" {
55-
logrus.Error("Okta EndpointURL is Empty")
56-
}
57-
oktaUrl, err := url.Parse(util.GetConfig().Okta.EndpointURL)
58-
if err != nil {
59-
return nil, errors.Wrap(err, "could not parse Okta EndpointURL")
60-
}
61-
oktaBaseURL := strings.Replace(oktaUrl.String(), oktaUrl.RequestURI(), "", 1)
62-
oAuthUserInfoReq, err := a.config.Client(context.Background(), oAuthToken).Get(oktaBaseURL + "/api/v1/users/me")
63-
if err != nil {
64-
return nil, errors.Wrap(err, "could not get user data")
65-
}
66-
defer oAuthUserInfoReq.Body.Close()
67-
var oUser struct {
68-
ID int `json:"sub"`
69-
// Custom URL property for user Avatar can go here
70-
Name string `json:"name"`
71-
}
72-
if err = json.NewDecoder(oAuthUserInfoReq.Body).Decode(&oUser); err != nil {
73-
return nil, errors.Wrap(err, "decoding user info failed")
74-
}
75-
return &user{
76-
ID: string(oUser.ID),
77-
Name: oUser.Name,
78-
Picture: util.GetConfig().BaseURL + "/images/okta_logo.png", // Default Okta Avatar
79-
}, nil
50+
logrus.Debugf("Getting User Data with state: %s, and code: %s", state, code)
51+
oAuthToken, err := a.config.Exchange(context.Background(), code)
52+
if err != nil {
53+
return nil, errors.Wrap(err, "could not exchange code")
54+
}
55+
if util.GetConfig().Okta.EndpointURL == "" {
56+
logrus.Error("Okta EndpointURL is Empty")
57+
}
58+
oktaUrl, err := url.Parse(util.GetConfig().Okta.EndpointURL)
59+
if err != nil {
60+
return nil, errors.Wrap(err, "could not parse Okta EndpointURL")
61+
}
62+
oktaBaseURL := strings.Replace(oktaUrl.String(), oktaUrl.RequestURI(), "", 1)
63+
oAuthUserInfoReq, err := a.config.Client(context.Background(), oAuthToken).Get(oktaBaseURL + "/api/v1/users/me")
64+
if err != nil {
65+
return nil, errors.Wrap(err, "could not get user data")
66+
}
67+
defer oAuthUserInfoReq.Body.Close()
68+
var oUser struct {
69+
ID int `json:"sub"`
70+
// Custom URL property for user Avatar can go here
71+
Name string `json:"name"`
72+
}
73+
if err = json.NewDecoder(oAuthUserInfoReq.Body).Decode(&oUser); err != nil {
74+
return nil, errors.Wrap(err, "decoding user info failed")
75+
}
76+
return &user{
77+
ID: fmt.Sprint(oUser.ID),
78+
Name: oUser.Name,
79+
Picture: util.GetConfig().BaseURL + "/images/okta_logo.png", // Default Okta Avatar
80+
}, nil
8081
}
8182

8283
func (a *oktaAdapter) GetOAuthProviderName() string {
83-
return "okta"
84+
return "okta"
8485
}

internal/handlers/public_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func TestHandleInfo(t *testing.T) {
177177
t.Fatalf("could not read the body: %v", err)
178178
}
179179
raw := makeJSON(t, gin.H{
180-
"error": "Key: '.ID' Error:Field validation for 'ID' failed on the 'required' tag",
180+
"error": "Key: 'ID' Error:Field validation for 'ID' failed on the 'required' tag",
181181
})
182182
if string(body) != raw {
183183
t.Fatalf("body is not the expected one: %s", body)

internal/stores/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func generateRandomString(length int) (string, error) {
196196
}
197197
n := num.Int64()
198198
if unicode.IsLetter(rune(n)) {
199-
result += string(n)
199+
result += string([]rune{rune(n)})
200200
}
201201
}
202202
return result, nil

0 commit comments

Comments
 (0)