Files
threshold/internal/auth/oidc/service_start_auth.go
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUS-1.1

* Rerun make gen

This will pick up the last of the license changes

* Revert "Rerun make gen"

This reverts commit dbe0968bffd92745d8663c6940debae098395a6b.

* Remove copywrite from generation script

The current version of copywrite is not sophisticated enough
to only apply the copywrite changes to certain subdirectories,
so to avoid accidentally relicensing our api and sdk modules,
we will stop running the copywrite script until such time
that it can do the right thing.

* License protobuf API as MPL

The protobuf API files are used to generate the SDK,
since we want the SDK to be MPL, the protobuf files
must also be MPL. This extends to all protobuf files
whose sources end up in the SDK directory, including:

- internal/proto/controller/api
- internal/proto/controller/custom_options
- internal/proto/plugin

It also includes the generated service directory in
internal/gen/controller/api/services, since they are
generated from the protobuf sources used for the SDK.

* Change website license file name

This makes the naming scheme consistent with the rest
of the repository.

* Add note on licensing to README

* Set copywrite license to BUSL

* Revert "Add note on licensing to README"

This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe.

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2023-08-11 10:31:53 -07:00

151 lines
5.2 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package oidc
import (
"context"
"fmt"
"net/url"
"time"
"github.com/hashicorp/boundary/internal/auth/oidc/request"
"github.com/hashicorp/boundary/internal/authtoken"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/cap/oidc"
"google.golang.org/protobuf/types/known/timestamppb"
)
// StartAuth accepts a request to start an OIDC authentication/authorization
// attempt. It returns two URLs and a tokenId. authUrl is an OIDC authorization
// request URL. The authUrl includes a "state" parameter which is encrypted and
// has a payload which includes (among other things) the final redirect
// (calculated from the clientInfo), a token_request_id, and nonce. The tokenUrl
// is the URL theclient can use to retrieve the results of the user's OIDC
// authentication attempt. The tokenId is an encrypted payload for the POST
// request to the tokenUrl.
//
// If the auth method is in an InactiveState, then an error is returned.
//
// Options supported:
//
// WithRoundTripPayload(string) provides an option for a client roundtrip
// payload. This payload will be added to the final redirect as a query
// parameter.
func StartAuth(ctx context.Context, oidcRepoFn OidcRepoFactory, authMethodId string, opt ...Option) (authUrl *url.URL, tokenId string, e error) {
const op = "oidc.StartAuth"
if authMethodId == "" {
return nil, "", errors.New(ctx, errors.InvalidParameter, op, "missing auth method id")
}
if oidcRepoFn == nil {
return nil, "", errors.New(ctx, errors.InvalidParameter, op, "missing oidc repo function")
}
r, err := oidcRepoFn()
if err != nil {
return nil, "", errors.Wrap(ctx, err, op)
}
am, err := r.lookupAuthMethod(ctx, authMethodId)
if err != nil {
return nil, "", errors.Wrap(ctx, err, op)
}
if am == nil {
return nil, "", errors.New(ctx, errors.RecordNotFound, op, fmt.Sprintf("auth method %s not found", authMethodId))
}
if am.OperationalState == string(InactiveState) {
return nil, "", errors.New(ctx, errors.AuthMethodInactive, op, "not allowed to start authentication attempt")
}
// get the provider from the cache (if possible)
provider, err := providerCache().get(ctx, am)
if err != nil {
return nil, "", errors.Wrap(ctx, err, op)
}
callbackRedirect := fmt.Sprintf(CallbackEndpoint, am.GetApiUrl())
opts := getOpts(opt...)
finalRedirect := fmt.Sprintf(FinalRedirectEndpoint, am.GetApiUrl())
if opts.withRoundtripPayload != "" {
u := make(url.Values)
u.Add("roundtrip_payload", opts.withRoundtripPayload)
finalRedirect = fmt.Sprintf("%s?%s", finalRedirect, u.Encode())
}
now := time.Now()
createTime := timestamppb.New(now.Truncate(time.Second))
exp := timestamppb.New(now.Add(AttemptExpiration).Truncate(time.Second))
tokenRequestId, err := authtoken.NewAuthTokenId(ctx)
if err != nil {
return nil, "", errors.Wrap(ctx, err, op)
}
nonce, err := oidc.NewID()
if err != nil {
return nil, "", errors.New(ctx, errors.Unknown, op, "unable to generate nonce", errors.WithWrap(err))
}
hash, err := provider.ConfigHash()
if err != nil {
return nil, "", errors.New(ctx, errors.Unknown, op, "unable to get provider config hash", errors.WithWrap(err))
}
st := &request.State{
TokenRequestId: tokenRequestId,
CreateTime: &timestamp.Timestamp{Timestamp: createTime},
ExpirationTime: &timestamp.Timestamp{Timestamp: exp},
FinalRedirectUrl: finalRedirect,
Nonce: nonce,
ProviderConfigHash: hash,
}
requestWrapper, err := requestWrappingWrapper(ctx, r.kms, am.ScopeId, authMethodId)
if err != nil {
return nil, "", errors.Wrap(ctx, err, op)
}
encodedEncryptedSt, err := encryptMessage(ctx, requestWrapper, am, st)
if err != nil {
return nil, "", errors.Wrap(ctx, err, op)
}
oidcOpts := []oidc.Option{
oidc.WithState(string(encodedEncryptedSt)),
oidc.WithNonce(nonce),
}
switch {
case am.MaxAge == -1:
oidcOpts = append(oidcOpts, oidc.WithMaxAge(0))
case am.MaxAge > 0:
oidcOpts = append(oidcOpts, oidc.WithMaxAge(uint(am.MaxAge)))
default:
}
if len(am.ClaimsScopes) > 0 {
oidcOpts = append(oidcOpts, oidc.WithScopes(am.ClaimsScopes...))
}
// a bare min oidc.Request needed for the provider.AuthURL(...) call. We've intentionally not populated
// things like Audiences, because this oidc.Request isn't cached and not intended for use in future legs
// of the authen flow.
oidcReq, err := oidc.NewRequest(
AttemptExpiration,
callbackRedirect,
oidcOpts...)
if err != nil {
return nil, "", errors.New(ctx, errors.Unknown, op, "unable to create oidc request", errors.WithWrap(err))
}
u, err := provider.AuthURL(ctx, oidcReq)
if err != nil {
return nil, "", errors.New(ctx, errors.Unknown, op, "unable to get auth url from provider", errors.WithWrap(err))
}
authUrl, err = url.Parse(u)
if err != nil {
return nil, "", errors.New(ctx, errors.Unknown, op, "unable to parse auth url", errors.WithWrap(err))
}
t := &request.Token{
RequestId: tokenRequestId,
ExpirationTime: &timestamp.Timestamp{Timestamp: exp},
}
encodedEncryptedTk, err := encryptMessage(ctx, requestWrapper, am, t)
if err != nil {
return nil, "", errors.Wrap(ctx, err, op)
}
return authUrl, encodedEncryptedTk, nil
}