Skip to content

Commit

Permalink
Merge pull request #58 from owncloud/auto_provision_accounts
Browse files Browse the repository at this point in the history
autoprovision new users on login
  • Loading branch information
butonic authored Jun 25, 2020
2 parents 13c8826 + 2491087 commit e5e9192
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/auto_provision_accounts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: create account if it doesn't exist in ocis-accounts

The accounts_uuid middleware tries to get the account from ocis-accounts.
If it doens't exist there yet the proxy creates the account using the ocis-account api.

https://github.com/owncloud/ocis-proxy/issues/55
31 changes: 29 additions & 2 deletions pkg/middleware/account_uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ func getAccount(l log.Logger, claims *oidc.StandardClaims, ac acc.AccountsServic
return
}

func createAccount(l log.Logger, claims *oidc.StandardClaims, ac acc.AccountsService) (*acc.Account, int) {
// TODO check if fields are missing.
req := &acc.CreateAccountRequest{
Account: &acc.Account{
DisplayName: claims.DisplayName,
PreferredName: claims.PreferredUsername,
Mail: claims.Email,
CreationType: "LocalAccount",
},
}
created, err := ac.CreateAccount(context.Background(), req)
if err != nil {
l.Error().Err(err).Interface("account", req.Account).Msg("could not create account")
return nil, http.StatusInternalServerError
}

return created, 0
}

// AccountUUID provides a middleware which mints a jwt and adds it to the proxied request based
// on the oidc-claims
func AccountUUID(opts ...Option) func(next http.Handler) http.Handler {
Expand Down Expand Up @@ -89,8 +108,16 @@ func AccountUUID(opts ...Option) func(next http.Handler) http.Handler {

account, status := getAccount(l, claims, opt.AccountsClient)
if status != 0 {
w.WriteHeader(status)
return
if status == http.StatusNotFound {
account, status = createAccount(l, claims, opt.AccountsClient)
if status != 0 {
w.WriteHeader(status)
return
}
} else {
w.WriteHeader(status)
return
}
}
if !account.AccountEnabled {
l.Debug().Interface("account", account).Msg("account is disabled")
Expand Down

0 comments on commit e5e9192

Please sign in to comment.