4
4
"errors"
5
5
"fmt"
6
6
"io"
7
- "log"
8
7
9
8
"net/http"
10
9
@@ -88,7 +87,7 @@ func AuthorizeCreate(env *activitypub.Env, w http.ResponseWriter, r *http.Reques
88
87
89
88
token := & models.Token {
90
89
AccessToken : uuid .New ().String (),
91
- AccountID : account .ID ,
90
+ AccountID : & account .ID ,
92
91
ApplicationID : app .ID ,
93
92
TokenType : models .TokenType ("Bearer" ),
94
93
Scope : "read write follow push" ,
@@ -110,32 +109,59 @@ func TokenCreate(env *activitypub.Env, w http.ResponseWriter, r *http.Request) e
110
109
ClientID string `json:"client_id" schema:"client_id,required"`
111
110
ClientSecret string `json:"client_secret" schema:"client_secret,required"`
112
111
GrantType string `json:"grant_type" schema:"grant_type,required"`
113
- Code string `json:"code" schema:"code,required "`
112
+ Code string `json:"code" schema:"code"`
114
113
RedirectURI string `json:"redirect_uri" schema:"redirect_uri,required"`
115
- Scope string `json:"- " schema:"scope"` // ignored
114
+ Scope string `json:"scope " schema:"scope"`
116
115
}
117
116
if err := httpx .Params (r , & params ); err != nil {
118
117
return err
119
118
}
120
- var token models.Token
121
- if err := env .DB .Where ("authorization_code = ?" , params .Code ).First (& token ).Error ; err != nil {
122
- return httpx .Error (http .StatusUnauthorized , fmt .Errorf ("token with code %s not found" , params .Code ))
123
- }
119
+
124
120
var app models.Application
125
121
if err := env .DB .Where ("client_id = ?" , params .ClientID ).First (& app ).Error ; err != nil {
126
122
return httpx .Error (http .StatusBadRequest , fmt .Errorf ("failed to find application: %w" , err ))
127
123
}
124
+ if app .ClientSecret != params .ClientSecret {
125
+ return httpx .Error (http .StatusUnauthorized , fmt .Errorf ("invalid client_secret" ))
126
+ }
128
127
129
- if token .ApplicationID != app .ID {
130
- log .Println ("client_id mismatch" , token .ApplicationID , app .ID )
131
- return httpx .Error (http .StatusUnauthorized , fmt .Errorf ("client_id mismatch" ))
128
+ switch params .GrantType {
129
+ case "authorization_code" :
130
+ var token models.Token
131
+ if err := env .DB .Where ("authorization_code = ?" , params .Code ).First (& token ).Error ; err != nil {
132
+ return httpx .Error (http .StatusUnauthorized , fmt .Errorf ("token with code %s not found" , params .Code ))
133
+ }
134
+ if token .ApplicationID != app .ID {
135
+ return httpx .Error (http .StatusUnauthorized , fmt .Errorf ("client_id mismatch" ))
136
+ }
137
+ return to .JSON (w , map [string ]any {
138
+ "access_token" : token .AccessToken ,
139
+ "token_type" : token .TokenType ,
140
+ "scope" : token .Scope ,
141
+ "created_at" : token .CreatedAt .Unix (),
142
+ })
143
+ case "refresh_token" :
144
+ return httpx .Error (http .StatusNotImplemented , fmt .Errorf ("refresh_token grant type not implemented" ))
145
+ case "client_credentials" :
146
+ token := & models.Token {
147
+ AccessToken : uuid .New ().String (),
148
+ ApplicationID : app .ID ,
149
+ TokenType : models .TokenType ("Bearer" ),
150
+ Scope : params .Scope ,
151
+ AuthorizationCode : uuid .New ().String (),
152
+ }
153
+ if err := env .DB .Create (token ).Error ; err != nil {
154
+ return err
155
+ }
156
+ return to .JSON (w , map [string ]any {
157
+ "access_token" : token .AccessToken ,
158
+ "token_type" : token .TokenType ,
159
+ "scope" : token .Scope ,
160
+ "created_at" : token .CreatedAt .Unix (),
161
+ })
162
+ default :
163
+ return httpx .Error (http .StatusBadRequest , fmt .Errorf ("invalid grant_type" ))
132
164
}
133
- return to .JSON (w , map [string ]any {
134
- "access_token" : token .AccessToken ,
135
- "token_type" : token .TokenType ,
136
- "scope" : token .Scope ,
137
- "created_at" : token .CreatedAt .Unix (),
138
- })
139
165
}
140
166
141
167
func TokenDestroy (env * activitypub.Env , w http.ResponseWriter , r * http.Request ) error {
0 commit comments