Skip to content

Commit 8162739

Browse files
committed
Merge remote-tracking branch 'origin/v0.11' into v0.11
2 parents fb4400a + ef526ba commit 8162739

File tree

14 files changed

+139
-26
lines changed

14 files changed

+139
-26
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: Set up Go
13-
uses: actions/setup-go@v3
13+
uses: actions/setup-go@v5
1414
with:
1515
go-version: 1.23.4
1616

1717
- name: Check out code
18-
uses: actions/checkout@v2
18+
uses: actions/checkout@v4
1919

2020
- name: Setup node
2121
uses: actions/setup-node@v4

.github/workflows/release.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ jobs:
1212
env:
1313
DOCKER_CLI_EXPERIMENTAL: "enabled"
1414
steps:
15-
- uses: actions/checkout@v2
15+
- uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
1618
- name: Set up QEMU
1719
uses: docker/setup-qemu-action@v3
1820
- name: Set up Docker Buildx
@@ -52,7 +54,9 @@ jobs:
5254
release-windows:
5355
runs-on: windows-latest
5456
steps:
55-
- uses: actions/checkout@v2
57+
- uses: actions/checkout@v4
58+
with:
59+
fetch-depth: 0
5660
- uses: actions/setup-go@v5
5761
with:
5862
go-version: 1.23.4

.goreleaser.windows.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ builds:
44
- env:
55
- CGO_ENABLED=0
66
ldflags:
7+
- -s
8+
- -w
79
- -X mokapi/version.BuildVersion={{ .Version }}
810
- -X mokapi/version.BuildTime={{ .Date }}
911
main: ./cmd/mokapi
@@ -13,11 +15,11 @@ builds:
1315
- amd64
1416
archives:
1517
- name_template: '{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}'
16-
format: zip
18+
formats: [ 'zip' ]
1719
checksum:
1820
name_template: "{{ .ProjectName }}_v{{ .Version }}_windows_checksums.txt"
1921
snapshot:
20-
name_template: "{{ .Version }}"
22+
version_template: "{{ .Version }}"
2123

2224
chocolateys:
2325
- name: mokapi

.goreleaser.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ builds:
44
- env:
55
- CGO_ENABLED=0
66
ldflags:
7+
- -s
8+
- -w
79
- -X mokapi/version.BuildVersion={{ .Version }}
810
- -X mokapi/version.BuildTime={{ .Date }}
911
main: ./cmd/mokapi

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ tasks:
1010
build-windows:
1111
deps: [build-vue-app]
1212
cmds:
13-
- go build -o mokapi-windows-amd64.exe -ldflags="-X 'mokapi/version.BuildVersion=1.0'" ./cmd/mokapi
13+
- go build -o mokapi-windows-amd64.exe -ldflags="-w -s -X 'mokapi/version.BuildVersion=1.0'" ./cmd/mokapi
1414
env:
1515
GOOS: windows
1616
GOARCH: amd64

api/handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func writeJsonBody(w http.ResponseWriter, i interface{}) {
207207
b, err := json.Marshal(i)
208208
if err != nil {
209209
writeError(w, err, http.StatusInternalServerError)
210+
return
210211
}
211212
_, err = w.Write(b)
212213
if err != nil {

api/handler_http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func TestHandler_Http(t *testing.T) {
292292
}
293293
},
294294
requestUrl: "http://foo.api/api/services/http/foo",
295-
responseBody: `{"name":"","paths":[{"path":"/foo/{bar}","operations":[{"method":"get","deprecated":false,"responses":[{"statusCode":"200","description":"foo description","contents":[{"type":"application/json","schema":{"type":"object","properties":{"loop":{"ref":"#/components/schemas/loop","type":"object","properties":{"loop":{"ref":"#/components/schemas/loop"}}}}}}]}]}]}]}`,
295+
responseBody: `{"name":"","paths":[{"path":"/foo/{bar}","operations":[{"method":"get","deprecated":false,"responses":[{"statusCode":"200","description":"foo description","contents":[{"type":"application/json","schema":{"type":"object","properties":{"loop":{"$ref":"#/components/schemas/loop","type":"object","properties":{"loop":{"$ref":"#/components/schemas/loop"}}}}}}]}]}]}]}`,
296296
},
297297
{
298298
name: "schema with default",

config/static/static_config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ func (c *Config) Parse() error {
187187
case "":
188188
c.Providers.File.Filenames = append(c.Providers.File.Filenames, arg)
189189
default:
190-
return fmt.Errorf("positional argument is not supported: %v", arg)
190+
if u.Opaque != "" {
191+
c.Providers.File.Filenames = append(c.Providers.File.Filenames, arg)
192+
} else {
193+
return fmt.Errorf("positional argument is not supported: %v", arg)
194+
}
191195
}
192196
}
193197
return nil

config/static/static_config_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ providers:
512512
},
513513
},
514514
{
515-
name: "shorthand file",
515+
name: "positional parameter file",
516516
test: func(t *testing.T) {
517517
os.Args = append(os.Args, "mokapi.exe", "foo.json")
518518

@@ -527,7 +527,7 @@ providers:
527527
},
528528
},
529529
{
530-
name: "shorthand http",
530+
name: "positional parameter http",
531531
test: func(t *testing.T) {
532532
os.Args = append(os.Args, "mokapi.exe", "http://foo.io/foo.json")
533533

@@ -541,7 +541,7 @@ providers:
541541
},
542542
},
543543
{
544-
name: "shorthand https",
544+
name: "positional parameter https",
545545
test: func(t *testing.T) {
546546
os.Args = append(os.Args, "mokapi.exe", "https://foo.io/foo.json")
547547

@@ -555,7 +555,7 @@ providers:
555555
},
556556
},
557557
{
558-
name: "shorthand git with https",
558+
name: "positional parameter git with https",
559559
test: func(t *testing.T) {
560560
os.Args = append(os.Args, "mokapi.exe", "git+https://foo.io/foo.json")
561561

@@ -569,7 +569,7 @@ providers:
569569
},
570570
},
571571
{
572-
name: "shorthand npm",
572+
name: "positional parameter npm",
573573
test: func(t *testing.T) {
574574
os.Args = append(os.Args, "mokapi.exe", "npm://bar/foo.txt?scope=@foo")
575575

@@ -583,7 +583,7 @@ providers:
583583
},
584584
},
585585
{
586-
name: "shorthand not supported",
586+
name: "positional parameter not supported",
587587
test: func(t *testing.T) {
588588
os.Args = append(os.Args, "mokapi.exe", "foo://bar")
589589

@@ -594,6 +594,18 @@ providers:
594594
require.EqualError(t, err, "positional argument is not supported: foo://bar")
595595
},
596596
},
597+
{
598+
name: "positional parameter Windows path",
599+
test: func(t *testing.T) {
600+
os.Args = append(os.Args, "mokapi.exe", "C:\\bar")
601+
602+
cfg := static.Config{}
603+
err := decoders.Load([]decoders.ConfigDecoder{decoders.NewFlagDecoder()}, &cfg)
604+
require.NoError(t, err)
605+
err = cfg.Parse()
606+
require.Equal(t, "C:\\bar", cfg.Providers.File.Filenames[0])
607+
},
608+
},
597609
}
598610

599611
for _, tc := range testcases {

docs/examples/tutorials/mock-ldap-group-permission.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@ icon: bi-shield-lock
88

99
## Introduction
1010

11-
In this tutorial, you'll learn how to use Mokapi to mock an LDAP server for authentication and group-based access
12-
control in a Node.js backend. This is useful for testing without relying on a real LDAP
13-
server.
11+
LDAP authentication is widely used for managing user access in enterprise applications. However, setting up a real LDAP
12+
server for testing can be complex and time-consuming. In this tutorial, you will learn how to use Mokapi to mock an
13+
LDAP server and implement group-based authentication in a Node.js backend.
1414

1515
### What You'll Learn
1616

17-
- Set up a mock LDAP server using Mokapi
18-
- Authenticate users via LDAP in a Node.js backend
19-
- Implement group-based permissions
17+
- ✅ Setting up a mock LDAP server with Mokapi
18+
- ✅ Authenticating users via LDAP in a Node.js backend
19+
- ✅ Implementing group-based permissions
20+
- ✅ Testing authentication using cURL
21+
22+
## Prerequisites
23+
24+
Before starting, ensure you have the following:
25+
26+
- Node.js installed
27+
- Mokapi installed [Installation Guide](/docs/guides/get-started/installation.md)
28+
- Basic knowledge of LDAP authentication
2029

2130
## 1. Create an LDAP Mock Configuration
2231

@@ -66,9 +75,15 @@ Using [LDAP Browser](https://marketplace.visualstudio.com/items?itemName=fengtan
6675

6776
<img src="./vscode-ldap-browse-example.png" alt="Screenshot of VSCode displaying an LDAP directory structure." />
6877

69-
## 2. Implementing Backend in Node.js
78+
## 2. Implement LDAP Authentication in Node.js
79+
80+
Run the following command in install required dependencies:
81+
82+
```shell
83+
npm install ldapjs express
84+
```
7085

71-
Create a file `auth.js` and add:
86+
Create a file `auth.js` and add the following code:
7287

7388
```javascript
7489
const ldap = require('ldapjs');
@@ -101,6 +116,9 @@ function checkGroup(username, groupName, callback) {
101116
module.exports = { authenticate, checkGroup };
102117
```
103118

119+
- **authenticate**: This function binds the provided username and password to the mock LDAP server to verify authentication.
120+
- **checkGroup**: This function searches for the user to determine if they are a member of the Admins group.
121+
104122
In 'server.js' we use the LDAP authentication and group-based access control:
105123

106124
```javascript tab=server.js
@@ -129,6 +147,8 @@ app.post('/login', (req, res) => {
129147
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
130148
```
131149

150+
This Express.js server handles authentication and ensures only users in the Admins group can access protected resources.
151+
132152
## 3. Testing the Mock LDAP Authentication
133153

134154
Start Mokapi LDAP server:
@@ -170,4 +190,6 @@ If the username and/or password is not correct, the response will be:
170190
## Conclusion
171191

172192
You’ve successfully mocked an LDAP server with Mokapi, authenticated users, and
173-
implemented group-based permissions in a Node.js backend!
193+
implemented group-based permissions in a Node.js backend!
194+
195+
This setup allows you to test LDAP authentication and group permissions without needing a real LDAP server.

0 commit comments

Comments
 (0)