-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.go
More file actions
91 lines (77 loc) · 2.61 KB
/
oauth.go
File metadata and controls
91 lines (77 loc) · 2.61 KB
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
package oauth
import (
"github.com/agentstation/egothic"
"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
)
var (
// GetState gets the state from the oauth session.
GetState = egothic.GetState
// SetState sets the state in the oauth session.
SetState = egothic.SetState
// GetProviderName gets the provider name from the oauth session.
GetProviderName = egothic.GetProviderName
)
// SetStore sets the store for the oauth session.
func SetStore(store sessions.Store) {
egothic.SetStore(store)
}
// Store returns the store for the oauth session.
func Store() sessions.Store {
return egothic.Store()
}
// Begin starts the authentication process for a given provider.
func Begin(e echo.Context, opts ...Options) error {
eGothicOpts := optionsToEgothicOptions(opts...)
return egothic.BeginAuthHandler(e, eGothicOpts...)
}
// Complete completes the authentication process and returns the user.
func Complete(e echo.Context, opts ...Options) (User, error) {
eGothicOpts := optionsToEgothicOptions(opts...)
guser, err := egothic.CompleteUserAuth(e, eGothicOpts...)
if err != nil {
return User{}, err
}
// create a new oauth user from the goth user
user := User{
RawData: guser.RawData,
Provider: guser.Provider,
Email: guser.Email,
Name: guser.Name,
FirstName: guser.FirstName,
LastName: guser.LastName,
NickName: guser.NickName,
Description: guser.Description,
UserID: guser.UserID,
AvatarURL: guser.AvatarURL,
Location: guser.Location,
AccessToken: guser.AccessToken,
AccessTokenSecret: guser.AccessTokenSecret,
RefreshToken: guser.RefreshToken,
ExpiresAt: guser.ExpiresAt,
IDToken: guser.IDToken,
}
// validate that what we got back from the provider is usable
if err := user.Validate(); err != nil {
return User{}, err
}
return user, nil
}
// ProviderURL returns the authentication URL for the specified provider.
func ProviderURL(e echo.Context, opts ...Options) (string, error) {
eGothicOpts := optionsToEgothicOptions(opts...)
return egothic.GetAuthURL(e, eGothicOpts...)
}
// SetSession stores a key/value pair in the session.
func SetSession(e echo.Context, key string, value string) error {
return egothic.StoreInSession(e, key, value)
}
// GetSession retrieves a value from the session by key.
// It returns an error if the key doesn't exist.
func GetSession(e echo.Context, key string) (string, error) {
return egothic.GetFromSession(e, key)
}
// Logout invalidates a user session.
func Logout(e echo.Context) error {
return egothic.Logout(e)
}