Skip to content

Commit

Permalink
revocations: add auth token for api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
equinox0815 committed Nov 19, 2023
1 parent 20acf04 commit c61240b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
9 changes: 6 additions & 3 deletions cmd/whawty-nginx-sso/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ type LoginConfig struct {
}

type WebConfig struct {
Listen string `yaml:"listen"`
TLS *tlsconfig.TLSConfig `yaml:"tls"`
Login LoginConfig `yaml:"login"`
Listen string `yaml:"listen"`
TLS *tlsconfig.TLSConfig `yaml:"tls"`
Login LoginConfig `yaml:"login"`
Revocations struct {
Tokens []string `yaml:"tokens"`
} `yaml:"revocations"`
}

type Config struct {
Expand Down
23 changes: 22 additions & 1 deletion cmd/whawty-nginx-sso/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,28 @@ func (h *HandlerContext) handleSessions(c *gin.Context) {
}

func (h *HandlerContext) handleRevocations(c *gin.Context) {
// TODO: add authentication based on bearer tokens!
auth_header := c.GetHeader("Authorization")
if auth_header == "" {
c.JSON(http.StatusUnauthorized, WebError{"no authorization header found"})
return
}
auth_parts := strings.SplitN(auth_header, " ", 2)
if len(auth_parts) != 2 || auth_parts[0] != "Bearer" {
c.JSON(http.StatusUnauthorized, WebError{"authorization header is invalid"})
return
}
authenticated := false
for _, token := range h.conf.Revocations.Tokens {
if token == auth_parts[1] {
authenticated = true
break
}
}
if !authenticated {
c.JSON(http.StatusUnauthorized, WebError{"unauthorized token"})
return
}

revocations, err := h.cookies.ListRevoked()
if err != nil {
c.JSON(http.StatusInternalServerError, WebError{err.Error()})
Expand Down
4 changes: 4 additions & 0 deletions contrib/sample-cfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ web:
#### the http base path where the UI is hosted, if left empty the web interface will look for the HTTP header
#### X-BasePath and if this is empty as well '/' will be used.
# base-path: /sso/
revocations:
tokens:
- this-is-a-very-secret-token
- another-very-secret-token

# tls:
# certificate: "/path/to/server-crt.pem"
Expand Down
1 change: 0 additions & 1 deletion cookie/backend_in-memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func (b *InMemoryBackend) Save(username string, id ulid.ULID, session Session) e
b.sessions[username] = sessions
}
if _, exists = sessions[id]; exists {
// TODO: this probably should be a panic
return fmt.Errorf("session '%v' already exists!", id)
}
sessions[id] = session
Expand Down

0 comments on commit c61240b

Please sign in to comment.