|
| 1 | +package idpsync |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/golang-jwt/jwt/v4" |
| 9 | + "github.com/google/uuid" |
| 10 | + "golang.org/x/xerrors" |
| 11 | + |
| 12 | + "cdr.dev/slog" |
| 13 | + "github.com/coder/coder/v2/coderd/database" |
| 14 | + "github.com/coder/coder/v2/coderd/httpapi" |
| 15 | + "github.com/coder/coder/v2/codersdk" |
| 16 | + "github.com/coder/coder/v2/site" |
| 17 | +) |
| 18 | + |
| 19 | +// IDPSync is an interface, so we can implement this as AGPL and as enterprise, |
| 20 | +// and just swap the underlying implementation. |
| 21 | +// IDPSync exists to contain all the logic for mapping a user's external IDP |
| 22 | +// claims to the internal representation of a user in Coder. |
| 23 | +// TODO: Move group + role sync into this interface. |
| 24 | +type IDPSync interface { |
| 25 | + OrganizationSyncEnabled() bool |
| 26 | + // ParseOrganizationClaims takes claims from an OIDC provider, and returns the |
| 27 | + // organization sync params for assigning users into organizations. |
| 28 | + ParseOrganizationClaims(ctx context.Context, _ jwt.MapClaims) (OrganizationParams, *HTTPError) |
| 29 | + // SyncOrganizations assigns and removed users from organizations based on the |
| 30 | + // provided params. |
| 31 | + SyncOrganizations(ctx context.Context, tx database.Store, user database.User, params OrganizationParams) error |
| 32 | +} |
| 33 | + |
| 34 | +// AGPLIDPSync is the configuration for syncing user information from an external |
| 35 | +// IDP. All related code to syncing user information should be in this package. |
| 36 | +type AGPLIDPSync struct { |
| 37 | + Logger slog.Logger |
| 38 | + |
| 39 | + SyncSettings |
| 40 | +} |
| 41 | + |
| 42 | +type SyncSettings struct { |
| 43 | + // OrganizationField selects the claim field to be used as the created user's |
| 44 | + // organizations. If the field is the empty string, then no organization updates |
| 45 | + // will ever come from the OIDC provider. |
| 46 | + OrganizationField string |
| 47 | + // OrganizationMapping controls how organizations returned by the OIDC provider get mapped |
| 48 | + OrganizationMapping map[string][]uuid.UUID |
| 49 | + // OrganizationAssignDefault will ensure all users that authenticate will be |
| 50 | + // placed into the default organization. This is mostly a hack to support |
| 51 | + // legacy deployments. |
| 52 | + OrganizationAssignDefault bool |
| 53 | +} |
| 54 | + |
| 55 | +type OrganizationParams struct { |
| 56 | + // SyncEnabled if false will skip syncing the user's organizations. |
| 57 | + SyncEnabled bool |
| 58 | + // IncludeDefault is primarily for single org deployments. It will ensure |
| 59 | + // a user is always inserted into the default org. |
| 60 | + IncludeDefault bool |
| 61 | + // Organizations is the list of organizations the user should be a member of |
| 62 | + // assuming syncing is turned on. |
| 63 | + Organizations []uuid.UUID |
| 64 | +} |
| 65 | + |
| 66 | +func NewAGPLSync(logger slog.Logger, settings SyncSettings) *AGPLIDPSync { |
| 67 | + return &AGPLIDPSync{ |
| 68 | + Logger: logger.Named("idp-sync"), |
| 69 | + SyncSettings: settings, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// ParseStringSliceClaim parses the claim for groups and roles, expected []string. |
| 74 | +// |
| 75 | +// Some providers like ADFS return a single string instead of an array if there |
| 76 | +// is only 1 element. So this function handles the edge cases. |
| 77 | +func ParseStringSliceClaim(claim interface{}) ([]string, error) { |
| 78 | + groups := make([]string, 0) |
| 79 | + if claim == nil { |
| 80 | + return groups, nil |
| 81 | + } |
| 82 | + |
| 83 | + // The simple case is the type is exactly what we expected |
| 84 | + asStringArray, ok := claim.([]string) |
| 85 | + if ok { |
| 86 | + return asStringArray, nil |
| 87 | + } |
| 88 | + |
| 89 | + asArray, ok := claim.([]interface{}) |
| 90 | + if ok { |
| 91 | + for i, item := range asArray { |
| 92 | + asString, ok := item.(string) |
| 93 | + if !ok { |
| 94 | + return nil, xerrors.Errorf("invalid claim type. Element %d expected a string, got: %T", i, item) |
| 95 | + } |
| 96 | + groups = append(groups, asString) |
| 97 | + } |
| 98 | + return groups, nil |
| 99 | + } |
| 100 | + |
| 101 | + asString, ok := claim.(string) |
| 102 | + if ok { |
| 103 | + if asString == "" { |
| 104 | + // Empty string should be 0 groups. |
| 105 | + return []string{}, nil |
| 106 | + } |
| 107 | + // If it is a single string, first check if it is a csv. |
| 108 | + // If a user hits this, it is likely a misconfiguration and they need |
| 109 | + // to reconfigure their IDP to send an array instead. |
| 110 | + if strings.Contains(asString, ",") { |
| 111 | + return nil, xerrors.Errorf("invalid claim type. Got a csv string (%q), change this claim to return an array of strings instead.", asString) |
| 112 | + } |
| 113 | + return []string{asString}, nil |
| 114 | + } |
| 115 | + |
| 116 | + // Not sure what the user gave us. |
| 117 | + return nil, xerrors.Errorf("invalid claim type. Expected an array of strings, got: %T", claim) |
| 118 | +} |
| 119 | + |
| 120 | +// IsHTTPError handles us being inconsistent with returning errors as values or |
| 121 | +// pointers. |
| 122 | +func IsHTTPError(err error) *HTTPError { |
| 123 | + var httpErr HTTPError |
| 124 | + if xerrors.As(err, &httpErr) { |
| 125 | + return &httpErr |
| 126 | + } |
| 127 | + |
| 128 | + var httpErrPtr *HTTPError |
| 129 | + if xerrors.As(err, &httpErrPtr) { |
| 130 | + return httpErrPtr |
| 131 | + } |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +// HTTPError is a helper struct for returning errors from the IDP sync process. |
| 136 | +// A regular error is not sufficient because many of these errors are surfaced |
| 137 | +// to a user logging in, and the errors should be descriptive. |
| 138 | +type HTTPError struct { |
| 139 | + Code int |
| 140 | + Msg string |
| 141 | + Detail string |
| 142 | + RenderStaticPage bool |
| 143 | + RenderDetailMarkdown bool |
| 144 | +} |
| 145 | + |
| 146 | +func (e HTTPError) Write(rw http.ResponseWriter, r *http.Request) { |
| 147 | + if e.RenderStaticPage { |
| 148 | + site.RenderStaticErrorPage(rw, r, site.ErrorPageData{ |
| 149 | + Status: e.Code, |
| 150 | + HideStatus: true, |
| 151 | + Title: e.Msg, |
| 152 | + Description: e.Detail, |
| 153 | + RetryEnabled: false, |
| 154 | + DashboardURL: "/login", |
| 155 | + |
| 156 | + RenderDescriptionMarkdown: e.RenderDetailMarkdown, |
| 157 | + }) |
| 158 | + return |
| 159 | + } |
| 160 | + httpapi.Write(r.Context(), rw, e.Code, codersdk.Response{ |
| 161 | + Message: e.Msg, |
| 162 | + Detail: e.Detail, |
| 163 | + }) |
| 164 | +} |
| 165 | + |
| 166 | +func (e HTTPError) Error() string { |
| 167 | + if e.Detail != "" { |
| 168 | + return e.Detail |
| 169 | + } |
| 170 | + |
| 171 | + return e.Msg |
| 172 | +} |
0 commit comments