@@ -18,6 +18,7 @@ import (
1818const (
1919 CookieTokenName = "token"
2020
21+ sessionKeyID = "id"
2122 sessionKeyProvider = "provider"
2223 sessionKeyToken = "token"
2324
@@ -36,8 +37,10 @@ func RegisterAuthEndpoint(e *echo.Echo, hashKey, blockKey []byte, providers map[
3637 }))
3738
3839 g .OPTIONS ("" , func (c echo.Context ) error { _ = c .NoContent (http .StatusNoContent ); return nil })
40+ g .GET ("" , a .listAuth )
3941 g .POST ("" , a .createAuth )
4042 g .PUT ("/:state" , a .updateAuth )
43+ g .DELETE ("/:state" , a .delete )
4144
4245 return a .AuthMiddleware ()
4346}
@@ -53,6 +56,7 @@ type authentication struct {
5356}
5457
5558type AuthResource struct {
59+ ID string `json:"id,omitempty"`
5660 Idp string `json:"idp"`
5761 Path string `json:"path,omitempty"`
5862 AuthorizationURL string `json:"authorizationUrl,omitempty"`
@@ -195,6 +199,7 @@ func (a *auth) updateAuth(c echo.Context) error {
195199 Path : "/" ,
196200 MaxAge : 86400 * 30 ,
197201 }
202+ ts .Values [sessionKeyID ] = state
198203 ts .Values [sessionKeyToken ] = token
199204 ts .Values [sessionKeyProvider ] = au .Idp
200205 err = ts .Save (c .Request (), c .Response ())
@@ -218,3 +223,70 @@ func (a *auth) updateAuth(c echo.Context) error {
218223
219224 return nil
220225}
226+
227+ func (a * auth ) listAuth (c echo.Context ) error {
228+ type list struct {
229+ Count int `json:"count"`
230+ Auths []string `json:"auths"`
231+ Included []interface {} `json:"@included,omitempty"`
232+ }
233+
234+ s , err := session .Get (CookieTokenName , c )
235+ if err != nil {
236+ _ = c .NoContent (http .StatusInternalServerError )
237+ return fmt .Errorf ("unable to get session: %w" , err )
238+ }
239+
240+ l := & list {
241+ Count : 0 ,
242+ Auths : []string {},
243+ }
244+
245+ id , ok := s .Values [sessionKeyID ].(string )
246+ if ! ok {
247+ return c .JSON (http .StatusOK , l )
248+ }
249+
250+ idp , ok := s .Values [sessionKeyProvider ].(string )
251+ if ! ok {
252+ return c .JSON (http .StatusOK , l )
253+ }
254+
255+ l .Count = 1
256+ l .Auths = append (l .Auths , "id" )
257+ l .Included = []interface {}{
258+ & AuthResource {
259+ ID : id ,
260+ Idp : idp ,
261+ },
262+ }
263+
264+ return c .JSON (http .StatusOK , l )
265+ }
266+
267+ func (a * auth ) delete (c echo.Context ) error {
268+ state := c .Param ("state" )
269+
270+ s , err := session .Get (CookieTokenName , c )
271+ if err != nil {
272+ _ = c .NoContent (http .StatusInternalServerError )
273+ return fmt .Errorf ("unable to get session: %w" , err )
274+ }
275+
276+ id , ok := s .Values [sessionKeyID ].(string )
277+ if ! ok {
278+ return c .NoContent (http .StatusNotFound )
279+ }
280+
281+ if id != state {
282+ return c .NoContent (http .StatusNotFound )
283+ }
284+
285+ s .Options = & sessions.Options {MaxAge : - 1 }
286+ err = s .Save (c .Request (), c .Response ())
287+ if err != nil {
288+ return fmt .Errorf ("unable to remove auth session: %w" , err )
289+ }
290+
291+ return c .NoContent (http .StatusNoContent )
292+ }
0 commit comments