Skip to content

Commit

Permalink
updating with origin main
Browse files Browse the repository at this point in the history
  • Loading branch information
sredxny committed Sep 6, 2023
2 parents bf5ed7a + 34930b2 commit d75bd0e
Show file tree
Hide file tree
Showing 100 changed files with 3,638 additions and 1,166 deletions.
24 changes: 0 additions & 24 deletions .github/workflows/go.yml

This file was deleted.

18 changes: 18 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: lint

on:
push:
branches: [ 'main' ]
pull_request:
branches: [ 'main' ]

jobs:
golangci:
name: Run golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.52.2
34 changes: 0 additions & 34 deletions .github/workflows/maint.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: test

on:
push:
branches: [ 'main' ]
pull_request:
branches: [ 'main' ]
jobs:
tests:
name: Run tests
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.17.x', '1.18.x', '1.19.x']
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- name: Go version
run: go version
- name: Run Go tests
run: |
go test -v ./...
34 changes: 14 additions & 20 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,35 @@

linters:
enable:
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports [fast: true, auto-fix: true]
- gosec # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: true, auto-fix: false]
- misspell # Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
- deadcode # Finds unused code [fast: true, auto-fix: false]
- golint # Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true, auto-fix: false]
- unconvert # Remove unnecessary type conversions [fast: true, auto-fix: false]

disable:
# TODO(ross): fix errors reported by these checkers and enable them
- bodyclose # checks whether HTTP response body is closed successfully [fast: false, auto-fix: false]
- depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false]
- dupl # Tool for code clone detection [fast: true, auto-fix: false]
- errcheck # Inspects source code for security problems [fast: true, auto-fix: false]
- gochecknoglobals # Checks that no globals are present in Go code [fast: true, auto-fix: false]
- gochecknoinits # Checks that no init functions are present in Go code [fast: true, auto-fix: false]
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
- gocritic # The most opinionated Go source code linter [fast: true, auto-fix: false]
- gocyclo # Computes and checks the cyclomatic complexity of functions [fast: true, auto-fix: false]
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports [fast: true, auto-fix: true]
- gosec # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: true, auto-fix: false]
- gosimple # Linter for Go source code that specializes in simplifying a code [fast: false, auto-fix: false]
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false, auto-fix: false]
- ineffassign # Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
- interfacer # Linter that suggests narrower interface types [fast: false, auto-fix: false]
- lll # Reports long lines [fast: true, auto-fix: false]
- maligned # Tool to detect Go structs that would take less memory if their fields were sorted [fast: true, auto-fix: false]
- misspell # Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
- prealloc # Finds slice declarations that could potentially be preallocated [fast: true, auto-fix: false]
- scopelint # Scopelint checks for unpinned variables in go programs [fast: true, auto-fix: false]
- revive # Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true, auto-fix: false]
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: false, auto-fix: false]
- structcheck # Finds unused struct fields [fast: true, auto-fix: false]
- stylecheck # Stylecheck is a replacement for golint [fast: false, auto-fix: false]
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code [fast: true, auto-fix: false]
- unconvert # Remove unnecessary type conversions [fast: true, auto-fix: false]
- unparam # Reports unused function parameters [fast: false, auto-fix: false]
- unused # Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
- varcheck # Finds unused global variables and constants [fast: true, auto-fix: false]

disable:
# TODO(ross): fix errors reported by these checkers and enable them
- dupl # Tool for code clone detection [fast: true, auto-fix: false]
- gochecknoglobals # Checks that no globals are present in Go code [fast: true, auto-fix: false]
- gochecknoinits # Checks that no init functions are present in Go code [fast: true, auto-fix: false]
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
- lll # Reports long lines [fast: true, auto-fix: false]
linters-settings:
goimports:
local-prefixes: github.com/crewjam/saml
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import (
)

func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", samlsp.AttributeFromContext(r.Context(), "cn"))
fmt.Fprintf(w, "Hello, %s!", samlsp.AttributeFromContext(r.Context(), "displayName"))
}

func main() {
Expand Down
1 change: 1 addition & 0 deletions example/idp/idp.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main contains an example identity provider implementation.
package main

import (
Expand Down
22 changes: 15 additions & 7 deletions example/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Link struct {
}

// CreateLink handles requests to create links
func CreateLink(c web.C, w http.ResponseWriter, r *http.Request) {
func CreateLink(_ web.C, w http.ResponseWriter, r *http.Request) {
account := r.Header.Get("X-Remote-User")
l := Link{
ShortLink: uniuri.New(),
Expand All @@ -42,22 +42,20 @@ func CreateLink(c web.C, w http.ResponseWriter, r *http.Request) {
links[l.ShortLink] = l

fmt.Fprintf(w, "%s\n", l.ShortLink)
return
}

// ServeLink handles requests to redirect to a link
func ServeLink(c web.C, w http.ResponseWriter, r *http.Request) {
func ServeLink(_ web.C, w http.ResponseWriter, r *http.Request) {
l, ok := links[strings.TrimPrefix(r.URL.Path, "/")]
if !ok {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
http.Redirect(w, r, l.Target, http.StatusFound)
return
}

// ListLinks returns a list of the current user's links
func ListLinks(c web.C, w http.ResponseWriter, r *http.Request) {
func ListLinks(_ web.C, w http.ResponseWriter, r *http.Request) {
account := r.Header.Get("X-Remote-User")
for _, l := range links {
if l.Owner == account {
Expand Down Expand Up @@ -145,14 +143,24 @@ func main() {

spURL := *idpMetadataURL
spURL.Path = "/services/sp"
http.Post(spURL.String(), "text/xml", bytes.NewReader(spMetadataBuf))
resp, err := http.Post(spURL.String(), "text/xml", bytes.NewReader(spMetadataBuf))

if err != nil {
panic(err)
}

if err := resp.Body.Close(); err != nil {
panic(err)
}

goji.Handle("/saml/*", samlSP)

authMux := web.New()
authMux.Use(samlSP.RequireAccount)
authMux.Get("/whoami", func(w http.ResponseWriter, r *http.Request) {
pretty.Fprintf(w, "%# v", r)
if _, err := pretty.Fprintf(w, "%# v", r); err != nil {
panic(err)
}
})
authMux.Post("/", CreateLink)
authMux.Get("/", ListLinks)
Expand Down
63 changes: 46 additions & 17 deletions example/trivial/trivial.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main contains an example service provider implementation.
package main

import (
Expand All @@ -6,14 +7,34 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"log"
"net/http"
"net/url"
"time"

"github.com/crewjam/saml/samlsp"
)

var samlMiddleware *samlsp.Middleware

func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", samlsp.AttributeFromContext(r.Context(), "cn"))
fmt.Fprintf(w, "Hello, %s!", samlsp.AttributeFromContext(r.Context(), "displayName"))
}

func logout(w http.ResponseWriter, r *http.Request) {
nameID := samlsp.AttributeFromContext(r.Context(), "urn:oasis:names:tc:SAML:attribute:subject-id")
url, err := samlMiddleware.ServiceProvider.MakeRedirectLogoutRequest(nameID, "")
if err != nil {
panic(err) // TODO handle error
}

err = samlMiddleware.Session.DeleteSession(w, r)
if err != nil {
panic(err) // TODO handle error
}

w.Header().Add("Location", url.String())
w.WriteHeader(http.StatusFound)
}

func main() {
Expand All @@ -26,30 +47,38 @@ func main() {
panic(err) // TODO handle error
}

rootURL, _ := url.Parse("http://localhost:8000")
idpMetadataURL, _ := url.Parse("https://samltest.id/saml/idp")

idpMetadata, err := samlsp.FetchMetadata(
context.Background(),
http.DefaultClient,
idpMetadataURL, err := url.Parse("https://samltest.id/saml/idp")
if err != nil {
panic(err) // TODO handle error
}
idpMetadata, err := samlsp.FetchMetadata(context.Background(), http.DefaultClient,
*idpMetadataURL)
if err != nil {
panic(err) // TODO handle error
}

samlSP, err := samlsp.New(samlsp.Options{
URL: *rootURL,
IDPMetadata: idpMetadata,
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
Certificate: keyPair.Leaf,
SignRequest: true,
})
rootURL, err := url.Parse("http://localhost:8000")
if err != nil {
panic(err) // TODO handle error
}

samlMiddleware, _ = samlsp.New(samlsp.Options{
URL: *rootURL,
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
Certificate: keyPair.Leaf,
IDPMetadata: idpMetadata,
SignRequest: true, // some IdP require the SLO request to be signed
})
app := http.HandlerFunc(hello)
http.Handle("/hello", samlSP.RequireAccount(app))
http.Handle("/saml/", samlSP)
http.ListenAndServe(":8000", nil)
slo := http.HandlerFunc(logout)

http.Handle("/hello", samlMiddleware.RequireAccount(app))
http.Handle("/saml/", samlMiddleware)
http.Handle("/logout", slo)

server := &http.Server{
Addr: ":8080",
ReadHeaderTimeout: 5 * time.Second,
}
log.Fatal(server.ListenAndServe())
}
31 changes: 31 additions & 0 deletions flate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package saml

import (
"compress/flate"
"fmt"
"io"
)

const flateUncompressLimit = 10 * 1024 * 1024 // 10MB

func newSaferFlateReader(r io.Reader) io.ReadCloser {
return &saferFlateReader{r: flate.NewReader(r)}
}

type saferFlateReader struct {
r io.ReadCloser
count int
}

func (r *saferFlateReader) Read(p []byte) (n int, err error) {
if r.count+len(p) > flateUncompressLimit {
return 0, fmt.Errorf("flate: uncompress limit exceeded (%d bytes)", flateUncompressLimit)
}
n, err = r.r.Read(p)
r.count += n
return n, err
}

func (r *saferFlateReader) Close() error {
return r.r.Close()
}
19 changes: 8 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
module github.com/crewjam/saml

go 1.13
go 1.16

require (
github.com/beevik/etree v1.1.0
github.com/crewjam/httperr v0.2.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5
github.com/form3tech-oss/jwt-go v3.2.2+incompatible
github.com/google/go-cmp v0.5.5
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/kr/pretty v0.3.0
github.com/kr/text v0.2.0 // indirect
github.com/dchest/uniuri v1.2.0
github.com/golang-jwt/jwt/v4 v4.4.3
github.com/google/go-cmp v0.5.9
github.com/kr/pretty v0.3.1
github.com/mattermost/xml-roundtrip-validator v0.1.0
github.com/pkg/errors v0.9.1 // indirect
github.com/russellhaering/goxmldsig v1.1.1
github.com/russellhaering/goxmldsig v1.3.0
github.com/stretchr/testify v1.8.1
github.com/zenazn/goji v1.0.1
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed
gotest.tools v2.2.0+incompatible
)
Loading

0 comments on commit d75bd0e

Please sign in to comment.