-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauth2_provider.go
86 lines (79 loc) · 2.89 KB
/
oauth2_provider.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
package main
import (
"encoding/json"
"fmt"
"net/url"
"github.com/Microkubes/authorization-server/app"
"github.com/Microkubes/authorization-server/security"
"github.com/Microkubes/microservice-security/auth"
oa2 "github.com/Microkubes/microservice-security/oauth2"
"github.com/keitaroinc/goa"
"github.com/keitaroinc/oauth2"
)
// Oauth2ProviderController implements the oauth2_provider resource.
type Oauth2ProviderController struct {
*goa.Controller
*oauth2.ProviderController
oa2.ClientService
oa2.TokenService
security.SessionStore
ConfirmAuthorizationURL string
}
// NewOauth2ProviderController creates a oauth2_provider controller.
func NewOauth2ProviderController(service *goa.Service, provider oauth2.Provider, clientService oa2.ClientService,
tokenService oa2.TokenService, sessionStore security.SessionStore, confirmAuthURL string) *Oauth2ProviderController {
return &Oauth2ProviderController{
Controller: service.NewController("Oauth2ProviderController"),
ProviderController: oauth2.NewProviderController(service, provider),
ClientService: clientService,
TokenService: tokenService,
SessionStore: sessionStore,
ConfirmAuthorizationURL: confirmAuthURL,
}
}
// Authorize runs the authorize action.
func (c *Oauth2ProviderController) Authorize(ctx *app.AuthorizeOauth2ProviderContext) error {
clientID := ctx.ClientID
authObj := auth.GetAuth(ctx.Context)
if authObj == nil {
return ctx.BadRequest(&app.OAuth2ErrorMedia{
Error: "invalid_request",
})
}
confirmation := security.AuthorizeClientData{}
c.SessionStore.GetAs("confirmation", &confirmation, ctx.Request)
if !confirmation.Confirmed {
confirmation.ClientID = ctx.ClientID
confirmation.AuthorizeRequest = fmt.Sprintf("%s?%s", ctx.Request.URL.Path, ctx.Request.URL.Query().Encode())
c.SessionStore.SetValue("confirmation", confirmation, ctx.ResponseWriter, ctx.Request)
//redirect to confirmation URL
ctx.ResponseWriter.Header().Set("Location", c.ConfirmAuthorizationURL)
ctx.ResponseWriter.WriteHeader(302)
return nil
}
err := c.ProviderController.Authorize(ctx, ctx.ResponseWriter, ctx.Request)
if err != nil {
fmt.Println("Error in authorization: ", err.Error())
return err
}
redirectURL := ctx.ResponseWriter.Header().Get("Location")
u, err := url.Parse(redirectURL)
if err != nil {
return err
}
code := u.Query().Get("code")
userData, err := json.Marshal(authObj)
if err != nil {
return err
}
err = c.ClientService.UpdateUserData(clientID, code, authObj.UserID, string(userData))
if err != nil {
fmt.Println("Error while updating client data:", err.Error())
}
return err
}
// GetToken runs the get_token action.
func (c *Oauth2ProviderController) GetToken(ctx *app.GetTokenOauth2ProviderContext) error {
p := ctx.Payload
return c.ProviderController.GetToken(ctx, ctx.ResponseWriter, p.GrantType, p.Code, p.RedirectURI, p.RefreshToken, p.Scope)
}