/
token.go
152 lines (132 loc) · 5.26 KB
/
token.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
ZAU Single Sign-On
Copyright (C) 2021 Daniel A. Hawton <daniel@hawton.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package v1
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"math/rand"
"net/http"
"os"
"time"
"github.com/dhawton/log4g"
"github.com/gin-gonic/gin"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jwt"
utils "github.com/vzau/common/utils"
"github.com/vzau/sso/database/models"
dbTypes "github.com/vzau/types/database"
)
type TokenRequest struct {
GrantType string `form:"grant_type"`
ClientId string `form:"client_id"`
ClientSecret string `form:"client_secret"`
Code string `form:"code"`
RedirectURI string `form:"redirect_uri"`
CodeVerifier string `form:"code_verifier"`
}
type TokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
CodeChallenge string `json:"code_challenge"`
CodeChallengeMethod string `json:"code_challenge_method"`
}
func PostToken(c *gin.Context) {
treq := TokenRequest{}
if err := c.ShouldBind(&treq); err != nil {
log4g.Category("controllers/token").Error("Invalid request, missing field(s)")
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_request"})
return
}
if treq.GrantType != "authorization_code" {
log4g.Category("controllers/token").Error("Grant type is not authorization code")
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_grant"})
return
}
login := dbTypes.OAuthLogin{}
if err := models.DB.Joins("Client").Where("code = ?", treq.Code).First(&login).Error; err != nil {
log4g.Category("controllers/token").Error(fmt.Sprintf("Code %s not found", treq.Code))
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_request"})
return
}
defer models.DB.Delete(&login)
if treq.ClientId == "" || treq.ClientSecret == "" {
// Not in query string, let's grab from Authorization header
auth := c.Request.Header.Get("Authorization")
if auth == "" {
log4g.Category("controllers/token").Error("Invalid client: no creds passed.")
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_client"})
return
}
if fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", login.Client.ClientId, login.Client.ClientSecret)))) != auth {
log4g.Category("controllers/token").Error("Invalid client: creds did not match.")
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_client"})
return
}
}
if treq.ClientId != login.Client.ClientId || treq.ClientSecret != login.Client.ClientSecret {
log4g.Category("controllers/token").Error(fmt.Sprintf("Invalid client: %s does not match %s", treq.ClientId, login.Client.ClientId))
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_client"})
return
}
if login.CodeChallengeMethod == "S256" {
hash := sha256.Sum256([]byte(treq.CodeVerifier))
if login.CodeChallenge != base64.RawURLEncoding.EncodeToString(hash[:]) {
log4g.Category("controllers/token").Error(fmt.Sprintf("Code Challenge failed"))
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_grant"})
return
}
}
keyset, err := jwk.Parse([]byte(os.Getenv("SSO_JWKS")))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
log4g.Category("controller/token").Error("Could not parse JWKs: %s", err.Error())
return
}
rand.Seed(time.Now().Unix())
key, ok := keyset.Get(rand.Intn(keyset.Len()))
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
log4g.Category("controllers/token").Error("Could not find current key in JWKs")
return
}
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
log4g.Category("controllers/token").Error("Could not find current key in JWKs")
return
}
token := jwt.New()
token.Set(jwt.IssuerKey, utils.Getenv("SSO_ISSUERKEY", "auth.chicagoartcc.org"))
token.Set(jwt.AudienceKey, login.Client.Name)
token.Set(jwt.SubjectKey, fmt.Sprint(login.CID))
token.Set(jwt.IssuedAtKey, time.Now())
token.Set(jwt.ExpirationKey, time.Now().Add((time.Hour * 24 * 7)).Unix())
signed, err := jwt.Sign(token, jwa.SignatureAlgorithm(key.Algorithm()), key)
if err != nil {
log4g.Category("controllers/token").Error("Failed to create JWT: " + err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"error": "invalid_grant"})
return
}
ret := TokenResponse{
AccessToken: string(signed),
ExpiresIn: int(time.Hour*24*7) / int(time.Second),
TokenType: "Bearer",
CodeChallenge: login.CodeChallenge,
CodeChallengeMethod: login.CodeChallengeMethod,
}
c.JSON(http.StatusOK, ret)
}